SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SUM() from SELECT CASE?


SUM() from SELECT CASE?

Poll
How to get the SUM() from CASE?

0% - 0 votes using SUM()
0% 0 votes
Member votes: 0, Guest votes: 0. You don't have permission to vote in this poll
Author
Message
yeeyeeliew
yeeyeeliew
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 15
Hi, I have below Select statement ... with 3 columns (DOC, PRODTYPE and TERM). In TERM field, there are 4 type of data (1,5,9,12). I need to display the COUNT() of 1 as 'YR' and COUNT() of 12 as 'MTH'. I also need another column with the total of these 2 counts ...How can I get the SUM() of Count of 'YR' and 'MTH'?



Select DOC, PRODTYPE, CASE TERM WHEN 1 THEN COUNT(CAST(TERM AS INT)) END as 'YR', CASE TERM WHEN 12 THEN COUNT(CAST(TERM AS INT)) END AS 'MTH'

GROUP BY DOC, PRODTYPE, TERM
epriddy
epriddy
SSC-Enthusiastic
SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)

Group: General Forum Members
Points: 135 Visits: 110
There's likely a much simpler solution, but it's Saturday morning and I'm not quite "clicking" Wink

Try this:

with TermCTE (Doc, ProdType, Term, TermType) as
(Select
Doc,
ProdType,
Term,
TermType = case when Term = 1 then 'Yr'
when Term = 12 then 'Mth'
else 'Other'
end
from Table_name)

, sumCTE (Doc, ProdType, Term, YrSum, MthSum)
as
(Select
Doc,
ProdType,
Term,
YrSum = (Select sum(Term) from TermCTE where TermType = 'Yr'),
MthSum = (Select sum(Term) from TermCTE where TermType = 'Mth')
from TermCTE)

Select
Doc,
ProdType,
Term,
YrSum,
MthSum,
Sum_of_both = YrSum + MthSum
from sumCTE
bitbucket-25253
bitbucket-25253
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15947 Visits: 25280
Here is another way that might work for you.

CREATE TABLE #Table_1
(
[Doc] [varchar](50) NULL,
[Prodtype] [varchar](50) NULL,
[TERM] [int] NULL,
[Yr] [int] NULL,
[MTH] [int] Null
)
INSERT INTO #Table_1
Select DOC, PRODTYPE,TERM,
CASE TERM
WHEN 1 THEN COUNT(CAST(TERM AS INT))
ELSE 0
END as 'YR',
CASE TERM
WHEN 12 THEN COUNT(CAST(TERM AS INT))
ELSE 0
END AS 'MTH'
FROM TABLE_1
GROUP BY DOC, PRODTYPE,TERM

SELECT Doc, Prodtype, SUM(YR) AS 'YR', SUM(Mth) AS 'Mth', SUM(Yr + MTH) AS 'Gtotal'
FROM #Table_1
GROUP BY Doc, Prodtype

DROP TABLE #Table_1

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
The Dixie Flatline
The Dixie Flatline
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13080 Visits: 6903
For another column, try this

SELECT DOC, PRODTYPE
,sum(CASE WHEN TERM = 1 THEN 1 ELSE 0 END) as 'YR'
,sum(CASE WHEN TERM = 12 THEN 1 ELSE 0 END) AS 'MTH'
,sum(CASE WHEN TERM IN (1,12) THEN 1 ELSE 0 END) as 'YRMTH'
From (table)
GROUP BY DOC, PRODTYPE

I'm not really sure why you're doing this with both case statements and with group by. Could you expose a small set of sample data and your desired results?

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search