Finding the values from same table that have value 1 and 2 but not 3 - sql

Hi I have the below table where I want to find the object_ida that I have format_id 1,3,11 and 12 but not 10. Could you please help?

You can use group by and having:
select object_id
from t
group by object_id
having sum(case when format_id = 1 then 1 else 0 end) > 0 and
sum(case when format_id = 3 then 1 else 0 end) > 0 and
sum(case when format_id = 11 then 1 else 0 end) > 0 and
sum(case when format_id = 12 then 1 else 0 end) > 0 and
sum(case when format_id = 10 then 1 else 0 end) = 0;
Each condition tests for one of the format_ids. The > 0 means that at least one is assigned to an object_id. The = 0 means that none are assigned.

Related

Grouping case when results

I have the following query working.
select sh.encounter_id
, claims_total_allowable_cost
, (case when proc_code_desc = 'Inj Cefazolin Sodium, 500mg' then 1 else 0 end) as Cefazolin
, (case when proc_code_desc = 'Inj Cefoxitin Sodium, 1 G' then 1 else 0 end) as Cefoxitin
, (case when proc_code_desc = 'Inj Fentanyl Citrate' then 1 else 0 end) as Fentanyl
, (case when proc_code_desc = 'Inj Hydromorphone, 4mg' then 1 else 0 end) as Hydro
, (case when proc_code_desc = 'Inj Ketorolac Trometha, 15mg' then 1 else 0 end) as Ketorolac
, (case when proc_code_desc = 'Inj, Ondansetron Hci, 1 Mg' then 1 else 0 end) as Ondansetron
, (case when proc_code_desc = 'Inj, Propofol, 10 Mg' then 1 else 0 end) as Propofol
, (case when proc_code_desc = 'Ringer*' then 1 else 0 end) as Ringers
from health as sh
where cohort_description = 'Lap '
and sh.admit_dtm >= '2018-10-01'::date
and sh.admit_dtm <= '2019-09-30'::date
I get a result like this one:
encounter_id claims_total_allowable_cost cefazolin cefoxitin fentanyl hydro ketorolac
121 4200.85 0 0 0 0 0
121 4200.85 0 0 0 0 0
121 4200.85 0 0 0 0 1
121 4200.85 0 0 0 1 0
I would like a result like this:
encounter_id claims_total_allowable_cost cefazolin cefoxitin fentanyl hydro ketorolac
121 4200.85 0 0 0 1 1
Any help would be greatly appreciated!!
Just add group by and max() to your existing query
Select column1, max(column2),
Max(column3) ...
From (your existing query)
Group by column1

Solution to insert sub-query with additional group by

I'm trying to merge two working SQL query's in Oracle SQL Developer but can't seem to get the sub's Group By's to play nicely. I want/expect to see separate totals for each row but I'm getting an overall total for all rows.
I tried adding the second query as sub-query.
Query 1:
SELECT SOURCE,
sum(case when status = 'C1' then 1 else 0 end) as "C1",
sum(case when status = 'C2' then 1 else 0 end) as "C2",
sum(case when status = 'C3' then 1 else 0 end) as "C3",
sum(case when status = 'C4' then 1 else 0 end) as "C4",
sum(case when status = 'C5' then 1 else 0 end) as "C5"
FROM TABLE.req
GROUP BY SOURCE
ORDER BY SOURCE;
Query 2 to be added to the above:
SELECT SOURCE, COUNT(REQ.SOURCE) AS "Done in 7 Days"
FROM TABLE.req REQ
JOIN TABLE.audit AUD ON REQ.ROW_ID = AUD.RECORD_ID
WHERE (AUD.LAST_UPD - REQ.CREATED) <= 7
AND REQ.STATUS = 'Complete'
GROUP BY SOURCE;
Tried Sub-Query:
SELECT SOURCE,
sum(case when status = 'C1' then 1 else 0 end) as "C1",
sum(case when status = 'C2' then 1 else 0 end) as "C2",
sum(case when status = 'C3' then 1 else 0 end) as "C3",
sum(case when status = 'C4' then 1 else 0 end) as "C4",
sum(case when status = 'C5' then 1 else 0 end) as "C5"
(SELECT SOURCE, COUNT(REQ.SOURCE)
FROM TABLE.req REQ
JOIN TABLE.audit AUD ON REQ.ROW_ID = AUD.RECORD_ID
WHERE (AUD.LAST_UPD - REQ.CREATED) <= 7
AND REQ.STATUS = 'Complete'
GROUP BY SOURCE) AS "Done in 7"
FROM TABLE.req
GROUP BY SOURCE
ORDER BY SOURCE;
Query 1 returns:
A 0 0 0 0 0
B 0 0 3026 26 2461
C 0 0 0 0 0
D 3 39 2 1 19
E 0 0 61156 0 79430
Query 2 returns:
A 2906
B 10
C 28
D 7
E 0
ACTUAL:
Sub-Query returns the additional Column BUT it's being totaled
A 0 0 0 0 0 2951
B 0 0 3026 26 2461 2951
C 0 0 0 0 0 2951
D 3 39 2 1 19 2951
E 0 0 61156 0 79430 2951
EXPECTED:
Sub-Query returns the additional Column BUT it's being totaled
A 0 0 0 0 0 2906
B 0 0 3026 26 2461 10
C 0 0 0 0 0 28
D 3 39 2 1 19 7
E 0 0 61156 0 79430 0
You seem to want a correlated subquery:
SELECT SOURCE,
sum(case when status = 'C1' then 1 else 0 end) as "C1",
sum(case when status = 'C2' then 1 else 0 end) as "C2",
sum(case when status = 'C3' then 1 else 0 end) as "C3",
sum(case when status = 'C4' then 1 else 0 end) as "C4",
sum(case when status = 'C5' then 1 else 0 end) as "C5",
(SELECT COUNT(*)
FROM TABLE.req REQ r2 JOIN
TABLE.audit a
ON r2.ROW_ID = a.RECORD_ID
WHERE r2.SOURCE = r.SOURCE AND
(a.LAST_UPD - r2.CREATED) <= 7 AND
r2.STATUS = 'Complete'
)
FROM TABLE.req r
GROUP BY SOURCE
ORDER BY SOURCE;

Count the number of column having non zero values

I have the following script,
SELECT COUNT(*) AS Total,
SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) AS 'TotalCount1',
SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) AS 'TotalCount2',
SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) AS 'TotalCount3',
SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) AS 'TotalCount4',
SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) AS 'TotalCount5',
SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) AS 'TotalCount6'
FROM [Party]
Please refer the screenshot as the output of the above script.
What I want:
I want a column after the Total as the total number of the column having nonzero values.
Like in the picture the values should be 2 as TotalCount1 and Totalcount3 have non zero values.
SELECT COUNT(*) AS Total,
...
...
CASE ( WHEN SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END)
as SumOfNonZeros
FROM [Party]
Or maybe simpler
SELECT COUNT(*) AS Total,
COUNT(CASE WHEN TypeId ='4' THEN 1 END) AS 'TotalCount1',
COUNT(CASE WHEN TypeId ='6' THEN 1 END) AS 'TotalCount2',
COUNT(CASE WHEN TypeId ='1' THEN 1 END) AS 'TotalCount3',
COUNT(CASE WHEN TypeId ='10' THEN 1 END) AS 'TotalCount4',
COUNT(CASE WHEN TypeId ='5' THEN 1 END) AS 'TotalCount5',
COUNT(CASE WHEN TypeId ='8' THEN 1 END) AS 'TotalCount6',
COUNT( DISTINCT CASE WHEN TypeId IN ('4', '6', '1', '10', '5', '8')
THEN TypeId
END ) as CountOfNonZeros
FROM [Party]
Using CASE expression over the selecting result, you can do this:
SELECT (
CASE WHEN TotalCount1 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount2 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount3 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount4 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount5 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount6 > 0 THEN 1 ELSE 0 END) AS Result
FROM (
SELECT COUNT(*) AS Total,
SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) AS 'TotalCount1',
SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) AS 'TotalCount2',
SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) AS 'TotalCount3',
SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) AS 'TotalCount4',
SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) AS 'TotalCount5',
SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) AS 'TotalCount6'
FROM [Party]
) A

How to do mathematical comparision with SQL CASE statement

I have a student table around 100k records and I have two types of data in it: student name and level type with selection values primary, secondary, intermediate & university
I want to filter out the student from this table, whose have count > 0, in all level primary, secondary, intermediate & university
I was able to find the sum for each student in each level using the following query
SELECT
student_id,
SUM(CASE WHEN lev_type = 'primary' THEN 1 ELSE 0 END) AS primary,
SUM(CASE WHEN lev_type = 'secondary' THEN 1 ELSE 0 END) AS secondary,
SUM(CASE WHEN lev_type = 'intermediate' THEN 1 ELSE 0 END) AS intermediate,
SUM(CASE WHEN level_type = 'university' THEN 1 ELSE 0 END) AS university
FROM
student_details
GROUP BY
student_id
and I am getting a result like (note that my result is 92242 row(s))
attendee_id primary secondary intermediate uni
student1 0 1 1 2
student2 0 1 1 0
student3 88 209 92 32
student4 0 1 1 0
student5 0 1 1 0
How to filter out student3 from this result?
You can simply add a where statement as follows:
SELECT student_id,
SUM(case when lev_type = 'primary' then 1 else 0 end) as primary,
SUM(case when lev_type = 'secondary' then 1 else 0 end) as secondary ,
SUM(case when lev_type = 'intermediate' then 1 else 0 end) as intermediate ,
SUM(case when lev_type = 'university' then 1 else 0 end) as university
FROM student_details
GROUP BY student_id
WHERE primary = 0 OR secondary = 0 OR intermediate = 0 OR university = 0
HAVING might get you what you want. For example:
SELECT student_id,
sum(case when lev_type = 'primary' then 1 else 0 end) as primary,
sum(case when lev_type = 'secondary' then 1 else 0 end) as secondary ,
sum(case when lev_type = 'intermediate' then 1 else 0 end) as intermediate ,
sum(case when level_type = 'university' then 1 else 0 end) as university
from student_details
group by student_id
-- Put the criteria here by which you want to filter
having sum(case when lev_type = 'primary' then 1 else 0 end) = 0
and sum(case when lev_type = 'secondary' then 1 else 0 end) = 0
and sum(case when lev_type = 'intermediate' then 1 else 0 end) = 0
and sum(case when level_type = 'university' then 1 else 0 end) = 0

how do I correctly use case when statement

Hej,
I needed help with a case when statement in SQL Server.
Basically, I got three products and when the sum is equal to 2, then I want it to it be counted as 1 else 0. I wanted to know if the logic is write with this code or can it be improved?
case when sum(hase=1 OR hasd=1 OR hasf=1)=2 then 1 else 0 end as Xavc
What I was trying with this code is this: The customer might not have all three products however, if he has two products or the three the three , then it is equal to 2 and count is 1.
Something like this?
SELECT
CASE
WHEN hase + hasd + hasf = 2 THEN 1
ELSE 0
END AS Xavc
I think you are trying to do something like this...
CASE WHEN SUM(CASE WHEN hase=1 THEN 1 ELSE 0 END)
+ SUM(CASE WHEN hasd=1 THEN 1 ELSE 0 END)
+ SUM(CASE WHEN hasf=1 THEN 1 ELSE 0 END) = 2
THEN 1 ELSE 0 END AS Xavc
In this case try this ..
CASE WHEN SUM(CASE WHEN hase=1 THEN 1 ELSE 0 END) + SUM(CASE WHEN hasd=1 THEN 1 ELSE 0 END) = 2
OR SUM(CASE WHEN hasd=1 THEN 1 ELSE 0 END) + SUM(CASE WHEN hasf=1 THEN 1 ELSE 0 END) = 2
OR SUM(CASE WHEN hase=1 THEN 1 ELSE 0 END) + SUM(CASE WHEN hasf=1 THEN 1 ELSE 0 END) = 2
THEN 1 ELSE 0 END AS Xavc