January 3, 2014 at 7:51 am
Hi, I want SUM of count of the number of records available in database for first WHEN in below query. For example if i have 2 records in database for first case(prd_cd='1') , 5 for second case(prd_cd='2') and 10 for third case(pr_cd='3') then I should get 17 as sum of count in diplay. Can someone help?
select CASE
WHEN prd_cd='1' or prd_cd='2' or pr_cd='3' THEN 'PROCESSED' <---- I am looking for this only
WHEN prd_cd='X' THEN ' NOT PROCESSED'
ELSE 'UNKNOWN' END AS "Key Check Points", count(*) as "Rules"
from Table 1 where date(crt_ts) = current date
group by prd_cd with ur ;
THanks!!
January 3, 2014 at 8:00 am
select CASE
WHEN prd_cd='1' or prd_cd='2' or pr_cd='3' THEN 'PROCESSED' <---- I am looking for this only
WHEN prd_cd='X' THEN ' NOT PROCESSED'
ELSE 'UNKNOWN' END AS "Key Check Points", count(*) as "Rules",
SUM(CASE WHEN prd_cd='1' or prd_cd='2' or pr_cd='3' THEN 1 ELSE 0 END) as YourCount
from Table 1 where date(crt_ts) = current date
group by prd_cd with ur ;
January 3, 2014 at 8:30 am
Laurie, I am getting below error. Also I am looking for SUM of COUNT not SUM of fields.
104: SQL0104N An unexpected token "SUM" was found following "". Expected tokens may include: ", FROM INTO". SQLSTATE=42601
January 3, 2014 at 8:37 am
Can you post your real code?
January 3, 2014 at 8:59 am
It's a DB2 error, Laurie
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 3, 2014 at 9:06 am
Yes - I was wondering...
January 4, 2014 at 2:32 am
Hi, can anyone help here?
Thanks!!
January 4, 2014 at 12:29 pm
nasy_mcs (1/4/2014)
Hi, can anyone help here?Thanks!!
Add a WHERE clause that limits prd_cd to only those values that you test for in the CASE statements and see if that helps return the answer you're looking for.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 6, 2014 at 10:20 am
Hi Jeff,
I have lot of WHEN condition in the query, I have provided two conditions only in my example.
Putting WHERE (prd_cd='1' or prd_cd='2' or pr_cd='3' ) will not be correct?
Thanks!!
January 6, 2014 at 11:19 am
here's my first guess on what you are after...some SUM(CASE statements to get your subtotals:
SELECT SUM(CASE
WHEN prd_cd='1' OR prd_cd='2' OR pr_cd='3'
THEN 1
ELSE 0
END) AS Processed,
SUM(CASE
WHEN prd_cd='X'
THEN 1
ELSE 0
END) AS NotProcessed,
SUM(CASE
WHEN prd_cd='1' OR prd_cd='2' OR pr_cd='3' or prd_cd='X'
THEN 0
ELSE 1
END) As UnKown,
COUNT(*) AS Rules
FROM WHERE CONVERT(date,crt_ts) = CONVERT(date,getdate())
GROUP BY prd_cd;
Lowell
Viewing 10 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy