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
Related
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;
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
I'm trying to run this but I keep getting, error
^ found ")" (at char 81) expecting a keyword
The char 81 is in the 2nd SELECT portion AS
There is clearly an keyword AS in there
SELECT full_name, clm_id,
CASE WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS '0TO30'
WHEN OPEN_DAYS BETWEEN 31 AND 60 THEN 1 ELSE 0 END) AS '31TO60'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS '61TO90'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS '91TO120'
WHEN OPEN_DAYS >= 120 THEN 1 ELSE 0 END) AS 'GreaterThan120'
ELSE 'OTHERS'
END AS DAYS_RANGE,
COUNT(*) as NUMBER_OF_DAYS
FROM
(
SELECT DATE(CURRENT_DATE) - DATE(RCVD_DT) AS Open_Days
FROM clm_claim_rds_vw
WHERE CLM_ID like 'CC%'
AND EXTNL_STAT_CD = 'SUBMITTED'
) A
LEFT JOIN mt_employee_current_vw
ON clm_claim_rds_vw.examined_by_empl_id = mt_employee_current_vw.employee_number
GROUP BY full_name
You have wrong closing ) after each END.. try removing
SELECT full_name, clm_id,
CASE WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END AS '0TO30'
WHEN OPEN_DAYS BETWEEN 31 AND 60 THEN 1 ELSE 0 END AS '31TO60'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END AS '61TO90'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END AS '91TO120'
WHEN OPEN_DAYS >= 120 THEN 1 ELSE 0 END AS 'GreaterThan120'
ELSE 'OTHERS'
END AS DAYS_RANGE,
COUNT(*) as NUMBER_OF_DAYS
FROM
(
SELECT DATE(CURRENT_DATE) - DATE(RCVD_DT) AS Open_Days
FROM clm_claim_rds_vw
WHERE CLM_ID like 'CC%'
AND EXTNL_STAT_CD = 'SUBMITTED'
) A
LEFT JOIN mt_employee_current_vw
ON clm_claim_rds_vw.examined_by_empl_id = mt_employee_current_vw.employee_number
GROUP BY full_name
I think you want conditional aggregation:
SELECT ec.full_name, ec.clm_id,
SUM(CASE WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS cnt_0TO30,
SUM(CASE WHEN OPEN_DAYS BETWEEN 31 AND 60 THEN 1 ELSE 0 END) AS cnt_31TO60,
SUM(CASE WHEN OPEN_DAYS BETWEEN 61 AND 90 THEN 1 ELSE 0 END) AS cnt_61TO90,
SUM(CASE WHEN OPEN_DAYS BETWEEN 91 AND 120 THEN 1 ELSE 0 END) AS cnt_91TO120,
SUM(CASE WHEN OPEN_DAYS >= 120 THEN 1 ELSE 0 END) AS cnt_GreaterThan120
SUM(CASE WHEN OPEN_DAYS IS NULL OR OPEN_DAYS < 0 THEN 1 ELSE 0 END) as cnt_Others,
COUNT(*) as NUMBER_OF_DAYS
FROM (SELECT DATE(CURRENT_DATE) - DATE(RCVD_DT) AS Open_Days
FROM clm_claim_rds_vw
WHERE CLM_ID like 'CC%' AND
EXTNL_STAT_CD = 'SUBMITTED'
) ccr LEFT JOIN
mt_employee_current_vw ec
ON ccw.examined_by_empl_id = ec.employee_number
GROUP BY ec.full_name, ec.clm_id;
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.
I'm working with SQL server 2008 and I built a table with this function:
SELECT COD1 + COD2 + COD3 AS CODFINAL,
SUM(CASE WHEN SIG1 = 'BCO' THEN 1 ELSE 0 END) AS A1,
SUM(CASE WHEN SIG1 = 'RIP' THEN 1 ELSE 0 END) AS B1,
SUM(CASE WHEN SIG1 = 'MIN' THEN 1 ELSE 0 END) AS B1,
SUM(CASE WHEN SIG1 = 'MAD' THEN 1 ELSE 0 END) AS B1,
SUM(CASE WHEN SIG1 = 'CPR' THEN 1 ELSE 0 END) AS PC1,
SUM(CASE WHEN SIG1 = 'MAR' THEN 1 ELSE 0 END) AS B1,
SUM(CASE WHEN SIG1 = 'LAC' THEN 1 ELSE 0 END) AS CLu
FROM DBO.DATABASE
And I obtained something like this:
CODFINAL A1 B1 B1 B1 PC1 B1 CLu
B12030 1 1 1 1 1 2 3
C12130 1 1 1 1 1 2 3
D12230 1 1 1 1 1 2 3
E12230 1 1 1 1 1 2 3
But I would like to have a table with a unique total value like this:
CODFINAL A1 B1 PC1 CLu
B12030 1 5 1 3
C12130 1 5 1 3
D12230 1 5 1 3
E12230 1 5 1 3
Could it be something like this?
WITH MyCodes
(
SELECT COD1 + COD2 + COD3 AS CODFINAL,
SUM(CASE WHEN SIG1 = 'BCO' THEN 1 ELSE 0 END) AS A1,
SUM(CASE WHEN SIG1 = 'RIP' THEN 1 ELSE 0 END) AS B1a,
SUM(CASE WHEN SIG1 = 'MIN' THEN 1 ELSE 0 END) AS B1b,
SUM(CASE WHEN SIG1 = 'MAD' THEN 1 ELSE 0 END) AS B1c,
SUM(CASE WHEN SIG1 = 'CPR' THEN 1 ELSE 0 END) AS PC1,
SUM(CASE WHEN SIG1 = 'MAR' THEN 1 ELSE 0 END) AS B1d,
SUM(CASE WHEN SIG1 = 'LAC' THEN 1 ELSE 0 END) AS CLu
FROM DBO.DATABASE
)
SELECT CODFINAL,A1,B1a+B1b+B1c+B1d AS B1, PC1,CLu FROM MyCodes
You can use in:
SELECT COD1 + COD2 + COD3 AS CODFINAL,
SUM(CASE WHEN SIG1 = 'BCO' THEN 1 ELSE 0 END) AS A1,
SUM(CASE WHEN SIG1 IN ('RIP', 'MIN', 'MAD', 'MAR') THEN 1 ELSE 0 END) AS B1,
SUM(CASE WHEN SIG1 = 'CPR' THEN 1 ELSE 0 END) AS PC1,
SUM(CASE WHEN SIG1 = 'LAC' THEN 1 ELSE 0 END) AS CLu
FROM DBO.DATABASE