I usually use sum(case) to get sum of some columns:
i.e. SUM(CASE WHEN over05 = 'OK' THEN 1 ELSE 0 END) AS OK_05
and this is perfect when I have a column with two values, but when I have a column where I have three values:
i.e. over 05 = '1' or 'X' or '2'
how can I do a sum(case)?
If you want all three values to return the same thing, you should use IN():
SUM(
CASE
WHEN over05 IN ('1', 'X', '2') THEN 1
ELSE 0 END
) AS OK_05
If you want each value to return something different, you should use multiple WHEN ... THEN :
SUM(
CASE
WHEN over05 = '1' THEN 1
WHEN over05 = 'X' THEN 2
WHEN over05 = '2' THEN 3
ELSE 0 END
) AS OK_05
Related
For the above table, I need to reduce the rows down to one per Filter ID and have all the possible yes/no values showing for that particular Filter Id
for example:
Filter ID
Outpatient Prescriptions
Opioid Outpatient Prescriptions
...
IP Pharmacy Medication Orders - Component Level
1
Yes
Yes
...
No
How is this achieved?
If I understand your question, for each partition of FilterID value, you want any field that has a yes to be aggregated up as 'Yes', otherwise 'No'. If you group by FilterID then you can handle the rollup using a CASE SUM CASE.
SELECT
FilterID,
Field1Response = CASE WHEN SUM(CASE WHEN Field1='Yes' THEN 1 ELSE 0 END) > 1 THEN 'Yes' ELSE 'No' END,
Field2Response = CASE WHEN SUM(CASE WHEN Field2='Yes' THEN 1 ELSE 0 END) > 1 THEN 'Yes' ELSE 'No' END ,
Field3Response = CASE WHEN SUM(CASE WHEN Field3='Yes' THEN 1 ELSE 0 END) > 1 THEN 'Yes' ELSE 'No' END
...
FROM
Data
GROUP BY
FilterID
By the nature of the data, you can also simply use a MAX. This is not a good habit of getting into because the values may change over time, however, if the values are always Y or N then you could simply use MAX:
SELECT
FilterID,
Field1Response = MAX(Field1),
Field2Response = MAX(Field1),
Field3Response = MAX(Field1)
...
FROM
Data
GROUP BY
FilterID
I need help How can I merge the column into a single column, here is my code, is this method is correct. I want to get the count of the selected row in the table for the columns.
SELECT
CAT_MGR,
SUM ( case when CAT_MGR = 'A' THEN 1 else 0 end ) AS DESIGN,
sum (case when CAT_MGR = 'b' THEN 1 else 0 END) AS DESIGN,
sum (case when CAT_MGR = 'c' THEN 1 else 0 END) AS DESIGN
from Table_A
GROUP BY
CAT_MGR
Can you guys help me I'm a beginner at SQL.
Thank you in advance
If you want just one row in the resultset, then remove the group by clause. Then, if you want to count the three cat mgr together, you can use in:
select
sum(case when cat_mgr = 'a' then 1 else 0 end ) as design_a,
sum(case when cat_mgr = 'b' then 1 else 0 end ) as design_b,
sum(case when cat_mgr = 'c' then 1 else 0 end ) as design_c,
sum(case when cat_mgr in ('a', 'b', 'c') then 1 else 0 end ) as design
from Table_a
You just need to make addion like below in order to get one column "Design"
SELECT
CAT_MGR,
SUM (case when CAT_MGR = 'A' THEN 1 else 0 end )
+ sum (case when CAT_MGR = 'b' THEN 1 else 0 END)
+ sum (case when CAT_MGR = 'c' THEN 1 else 0 END)
AS DESIGN
from TJD_CORE_CATPB_TB
GROUP BY
CAT_MGR
I have to Case statements that count the same column just with a differnt criteria. The problem i am having is that the first case creates a null entry in the Test2 column but then counts it on my second case statement and leaves the Test 1 column null. I would like to have both counts side by side instead of created a duplicate row.
select m.no,
Case when itemtype = 'S' THEN count(ITEMKEY) end as Test1,
case when ItemType='C' THEN count(ITEMKEY) END as Test2
from test m
I'm pretty sure you want conditional aggregation. The case expression is an argument to the aggregation function:
select m.no,
sum(case when itemtype = 'S' then 1 else 0 end) as test1,
sum(case when itemtype = 'C' then 1 else 0 end) as test2
from test m
group by m.no;
This assumes that itemKey is never null, so the count() is just counting rows.
Following query can display records which having Itemtype 'S' or 'C' with count of itemkey. if itemkey is null it will display 0 else count of item key
select m.no,
Case when isnull( m.itemtype = 'S',0) THEN (select count(a.ITEMKEY) from test a where a.itemtype = 'S' ) else 0 end as Test1,
case when isnull( m.itemtype='C',0) THEN (select count(b.ITEMKEY) from test b where b.itemtype = 'C') else 0 END as Test2
from test m
How do I get this to work? If the count is higher for singles i would like it to output yes and then no for viceversa.
IF
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "M"
<
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "S"
Print 'Yes'
ELSE
Print 'No';
You can't use IF inside a query, instead use a CASE expression with conditional aggregation:
SELECT
CASE WHEN SUM(CASE WHEN StudMaritalStatus = 'M' THEN 1 ELSE 0 END) <
SUM(CASE WHEN StudMaritalStatus = 'S' THEN 1 ELSE 0 END)
THEN 'Yes' ELSE 'No' END AS label
FROM students
I got three columns with binary values. Then i got a query which makes a string based on those values. I made this work using case. But it's quite huge (it's a part of a bigger query), and i was wondering maybe there is a better way of doing this?
SELECT (CASE
WHEN TableA.flag1 = 1 AND TableA.flag2 = 1 AND TableA.flag3 = 1 THEN 'CGR'
WHEN TableA.flag1 = 1 AND TableA.flag2 = 1 THEN 'CG'
WHEN TableA.flag1 = 1 AND TableA.flag3 = 1 THEN 'CR'
WHEN TableA.flag2 = 1 AND TableA.flag3 = 1 THEN 'GR'
WHEN TableA.flag1 = 1 THEN 'C'
WHEN TableA.flag2 = 1 THEN 'G'
WHEN TableA.flag3 = 1 THEN 'R'
ELSE 'nothing'
END)
FROM TableA
Im working on MSSQL 2000 server.
You can use left() instead.
select left('C', T.flag1)+
left('G', T.flag2)+
left('R', T.flag3)
from TableA as T
I don't know if this is "better" but it is more concise:
SELECT ((CASE WHEN TableA.flag1 = 1 THEN 'C' ELSE '' END) +
(CASE WHEN TableA.flag2 = 1 THEN 'G' ELSE '' END) +
(CASE WHEN TableA.flag3 = 1 THEN 'R' ELSE '' END)
)
FROM TableA;
Okay, this isn't exactly the same because you get '' instead of 'nothing'. But I think the empty string does a better job of representing "no flags" than 'nothing' does.
Try this:
SELECT
CASE WHEN flag1 = 1 THEN 'C' ELSE '' END +
CASE WHEN flag2 = 1 THEN 'G' ELSE '' END +
CASE WHEN flag3 = 1 THEN 'R' ELSE '' END
FROM TABLEA
Maybe you could use function and move this CASE statement to this function