Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 how to: sum expression

Author  Topic 

u2p_inst
Yak Posting Veteran

78 Posts

Posted - 2002-10-29 : 23:26:22
I have query like this:

SELECT BRNAME,NAMEGROUP1, SUM([00])AS DEC_LAST_YEAR, SUM([01])AS JAN
,(SUM([01])- SUM([00]))AS GW_JAN
,(SUM([02])- SUM([01]))AS GW_FEB
,(SUM([03])- SUM([02]))AS GW_MAR
,(SUM([04])- SUM([03]))AS GW_APR
,(SUM([05])- SUM([04]))AS GW_MAY
,(SUM([06])- SUM([05]))AS GW_JUN
,(SUM([07])- SUM([06]))AS GW_JUL
,(SUM([08])- SUM([07]))AS GW_AUG
,(SUM([09])- SUM([08]))AS GW_SEP
,(SUM([10])- SUM([09]))AS GW_OCT
,(SUM([11])- SUM([10]))AS GW_NOV
,(SUM([12])- SUM([11]))AS GW_DEC
FROM MONTHLY_AVERAGE_VIEW
WHERE NAMEGROUP1
IN(SELECT DISTINCT NAMEGROUP1
FROM MONTHLY_AVERAGE_VIEW
WHERE NAMEGROUP1)
GROUP BY NAMEGROUP1, BRNAME
ORDER BY BRNAME

how to make query to sum the fields, if the NAMEGROUP1(this is name of field) are "ONBS", "CIM","SBI" but if NAMEGROUP1 = "NTB","OFFS"
are not to suming

oh

nr
SQLTeam MVY

12543 Posts

Posted - 2002-10-30 : 04:35:24
sum (case when NAMEGROUP1 in ('ONBS', 'CIM','SBI') then field else 0 end)

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

u2p_inst
Yak Posting Veteran

78 Posts

Posted - 2002-10-30 : 04:51:52
can you give me script more specified

oh
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2002-10-30 : 05:21:50
in your query
SUM([01])AS JAN
becomes
sum (case when NAMEGROUP1 in ('ONBS', 'CIM','SBI') then [01] else 0 end)

and the same for the others.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -