how to increment count when GROUP by is used [message #443761] |
Wed, 17 February 2010 04:37  |
chaituu
Messages: 115 Registered: June 2008
|
Senior Member |
|
|
SNO column should be incremented depending upon the total records to be fetched.if i get 4 records then sno numbers should be 1,2,3,4.i dont want to put rownum also in the GROUP BY clause.how to increment the serial number?
SELECT JC.A,
ROWNUM SNO, --serial number
JC.B,
SUM(CHR.AMOUNT),
SUM(CHR.FINALAMOUNT),
JC.C,
JC.D,
JC.E,
JC.F,
JC.G
FROM CHARGES CHR
WHERE JC.B = '12111'
AND JC.STATUS = 'INVOICED'
GROUP BY JC.A, JC.B, JC.C, JC.D, JC.E, JC.F, JC.G;
|
|
|
|
|
|
|
|
Re: how to increment count when GROUP by is used [message #443775 is a reply to message #443771] |
Wed, 17 February 2010 05:05   |
 |
Michel Cadot
Messages: 68757 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
SQL> select deptno, job, count(*) nb,
2 row_number() over (order by deptno, job) overall_num,
3 row_number() over (partition by deptno order by job) indept_num
4 from emp
5 group by deptno, job
6 order by deptno, job
7 /
DEPTNO JOB NB OVERALL_NUM INDEPT_NUM
---------- --------- ---------- ----------- ----------
10 PRESIDENT 1 1 1
20 ANALYST 2 2 1
20 CLERK 2 3 2
20 MANAGER 1 4 3
30 CLERK 1 5 1
30 MANAGER 1 6 2
30 SALESMAN 4 7 3
7 rows selected.
Regards
Michel
|
|
|
Re: how to increment count when GROUP by is used [message #443781 is a reply to message #443775] |
Wed, 17 February 2010 05:27   |
chaituu
Messages: 115 Registered: June 2008
|
Senior Member |
|
|
Michel Cadot wrote on Wed, 17 February 2010 16:35SQL> select deptno, job, count(*) nb,
2 row_number() over (order by deptno, job) overall_num,
3 row_number() over (partition by deptno order by job) indept_num
4 from emp
5 group by deptno, job
6 order by deptno, job
7 /
DEPTNO JOB NB OVERALL_NUM INDEPT_NUM
---------- --------- ---------- ----------- ----------
10 PRESIDENT 1 1 1
20 ANALYST 2 2 1
20 CLERK 2 3 2
20 MANAGER 1 4 3
30 CLERK 1 5 1
30 MANAGER 1 6 2
30 SALESMAN 4 7 3
7 rows selected.
Regards
Michel
i have one clarification.what i understood about partition by is it will group the deptno and assign the ranks.
|
|
|
|