Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

How to sum up rows returned from a select statement

$
0
0

Below is the code that I am using. The desired goal is to return a single row that is the sum of m1, m2, and m3 for the criteria given at the end. At the moment, it is giving three rows. While I can copy them to excel and sum it there, looking to make this more efficient. I have been looking around online but I think that I am not phrasing it correctly. Seems quite simple but not able to solve this.

select
CASE
WHEN suppress = 0 then isnull (m1, 0)
ELSE '-1'
END as mnth1emp,
CASE
WHEN suppress = 0 THEN isnull (m2, 0)
ELSE '-1'
END as mnth2emp,
CASE
WHEN suppress = 0 THEN isnull (m3, 0)
ELSE '-1'
END as mnth3emp
from dbo.industry20172f
where area='000031' and (indcode='31' or indcode ='32' or indcode='33') and ownership='00'

Current result

m1 m2 m3

10 20 30

40 50 60

70 80 90

Desired result

m1 m2 m3

120 150 180


Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>