sql - Can we use same aggregate function more than once on same table field or column using Different filter conditions? -
i want use sum() function on 'amount' field in query 4 times on same field with different filters.
like
select date1,cc,bu, sum(amount),sum(amount),sum(amount),sum(amount) maintable<br> group cc,bu,date1
here
1st sum(amount)
should calculated when mode='011'
, mode='012'
maintable
2nd sum(amount)
should calculated when mode '03_'
, mode '05_'
maintable
3rd sum(amount)
should calculated when mode '10_'
maintable
4th sum(amount)
should calculated when (mode !='011')
, (mode !='012')
(mode not '03_')
, (mode not '05_')
maintable
how make happen? have tried in many ways couldn't result way wanted.
please me.
thank in advance.
you can use aggregate function case
:
select date1, cc, bu, sum(case when mode = '011' amount end) mode011, sum(case when mode = '012' amount end) mode012, sum(case when mode = '013' amount end) mode013, sum(case when mode = '014' amount end) mode014 maintable group cc,bu,date1;
or can use pivot function:
select date1, cc, bu, [011] mode011, [012] mode012, [013] mode013, [014] mode014 ( select date1, cc, bu, mode, amount maintable ) src pivot ( sum(amount) mode in ([011], [012], [013], [014]) ) piv
Comments
Post a Comment