Solution to insert sub-query with additional group by - sql

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;

Related

SQL group and condition sum in the last row

I have several items which belongs to different group, some are unknown group.
I need the sum of each group by date.
I just try below SQL but still not exactly what I want:
with A as (
SELECT PG, EMANAGER_DATE,
CASE WHEN (EMANAGER_DATE = '2021-09') THEN SUM(NET_VALUE_USD) ELSE 0 END AS "2021-09",
CASE WHEN (EMANAGER_DATE = '2021-10') THEN SUM(NET_VALUE_USD) ELSE 0 END AS "2021-10",
CASE WHEN (EMANAGER_DATE = '2021-11') THEN SUM(NET_VALUE_USD) ELSE 0 END AS "2021-11",
CASE WHEN (EMANAGER_DATE = '2021-12') THEN SUM(NET_VALUE_USD) ELSE 0 END AS "2021-12"
FROM table1
GROUP BY PG, EMANAGER_DATE
)
select PG, max("2021-09") AS "2021-09", max("2021-10") AS "2021-10", max("2021-11") AS "2021-11", max("2021-12") AS "2021-12" from A GROUP BY PG
DB: SAP HANA
Data:
Item
PG
NET_VALUE_USD
EMANAGER_DATE
1
1
100
2021-09
2
1
200
2021-10
3
2
300
2021-09
4
2
400
2021-09
5
3
500
2021-09
6
3
200
2021-11
7
3
300
2021-11
8
900
2021-09
Expected result:
PG
2021-09
2021-10
2021-11
?
900
0
0
1
100
200
0
2
700
0
0
3
500
0
500
total
2200
200
500
You can adjust your query and use GROUPING SETS:
SELECT (CASE WHEN GROUPING(PG) = 1 THEN 'Total' ELSE PG END) as PG,
SUM(CASE WHEN EMANAGER_DATE = '2021-09' THEN NET_VALUE_USD ELSE 0 END) AS "2021-09",
SUM(CASE WHEN EMANAGER_DATE = '2021-10' THEN NET_VALUE_USD ELSE 0 END) AS "2021-10",
SUM(CASE WHEN EMANAGER_DATE = '2021-11' THEN NET_VALUE_USD ELSE 0 END) AS "2021-11",
SUM(CASE WHEN EMANAGER_DATE = '2021-12' THEN NET_VALUE_USD ELSE 0 END) AS "2021-12"
FROM _SYS_BIC."APP_SCM/ZCV_APP_SCM_CTOS_PARENT_UNCONFIRMED_BACKLOG_P"
GROUP BY GROUPING SETS ( (PG), () );

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

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

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.

Obtain an specific table

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

Sub select & union all

SELECT C.ClientCaseNumber,
Sum(CASE
WHEN CA.CaseActionDefinitionId IN (28, 29, 30) THEN 1
ELSE 0
END) AS [Wezwania],
Sum(CASE
WHEN CA.CaseActionDefinitionId IN (14, 21) THEN 1
ELSE 0
END) AS [Kontakt],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 32 THEN 1
ELSE 0
END) AS [SMS],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 44 THEN 1
ELSE 0
END) AS [Zgon],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 49 THEN 1
ELSE 0
END) AS [Areszt],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 37 THEN 1
ELSE 0
END) AS [Odmowa],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 39 THEN 1
ELSE 0
END) AS [Podważa],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 99 THEN 1
ELSE 0
END) AS [Ugoda],
[Adres],
[Numer],
[Mail],
[Powód]
FROM (SELECT Notes AS [Adres]
FROM CaseActionHistory
WHERE CaseActionDefinitionId = 68
UNION ALL
SELECT Info AS [Numer]
FROM CaseActionHistory
WHERE CaseActionDefinitionId IN (54, 55, 56, 58,
59, 60, 61, 62, 63)
UNION ALL
SELECT Notes AS [Mail]
FROM CaseActionHistory
WHERE CaseActionDefinitionId = 66
UNION ALL
SELECT Description AS [Powód]
FROM CaseActionDefinition
JOIN CaseActionHistory AS C
ON DefinitionId = C.CaseActionDefinitionId
WHERE DefinitionId BETWEEN 70 AND 78) AS x
INNER JOIN CaseDetails AS C
ON x.CaseDetailId = C.CaseDetaislId
INNER JOIN CaseActionHistory AS CA
ON C.CaseDetailsId = CA.CaseDetailId
WHERE C.ClientId = '11'
GROUP BY C.ClientCaseNumber
I've got such query. As return shows error of invalid columns "CaseDetailId, CaseDetailsId, Mail, Numer, Powód".
http://oi39.tinypic.com/2vwy44n.jpg
That's more or less how the results should look like.
ClientCaseNumber is taken from table CaseDetails
All the sums are sums of code added to CaseActionHistory table.
Notes/Info are in CaseActionHistory table
Description is placed in CaseActionDefinition table.
Between tables there are such connections:
CaseDetails.CaseDetailId = CaseActionHistory.CaseDetailsId
CaseActionHistory.CaseActionDefinitionId = CaseActionDefinition.DefinitionId
The UNION clause does not work like that.
This query:
select Notes as [Adres] from CaseActionHistory where ...
UNION ALL
select Info as [Numer] from CaseActionHistory where ...
UNION ALL
select Notes as [Mail] from CaseActionHistory where ...
UNION ALL
select Description as [Powód] from CaseActionDefinition join CaseActionHistory ...
will not populate a table with 4 columns. Instead it will be a table with one column, with all the values one after the other. The name of the column will be taken from the first SELECT, i.e.
if the first query returns values 1 and 2,
the second query returns values 3 and 4
the third query returns values 5 and 6
the fourth query returns values 7 and 8
you wont get:
Adres | Numer | Mail | Powód
------------------------------
1 | 3 | 5 | 7
2 | 4 | 6 | 8
but you'll get:
Adres
-------
1
2
3
4
5
6
7
8
Are you not missing the alias 'x' from these fields:
SELECT
C.ClientCaseNumber
,sum(case when CA.CaseActionDefinitionId in (28,29,30) then 1 else 0 end) as [Wezwania]
,sum(case when CA.CaseActionDefinitionId in (14,21) then 1 else 0 end) as [Kontakt]
,sum(case when CA.CaseActionDefinitionId = 32 then 1 else 0 end) as [SMS]
,sum(case when CA.CaseActionDefinitionId = 44 then 1 else 0 end) as [Zgon]
,sum(case when CA.CaseActionDefinitionId = 49 then 1 else 0 end) as [Areszt]
,sum(case when CA.CaseActionDefinitionId = 37 then 1 else 0 end) as [Odmowa]
,sum(case when CA.CaseActionDefinitionId = 39 then 1 else 0 end) as [Podważa]
,sum(case when CA.CaseActionDefinitionId = 99 then 1 else 0 end) as [Ugoda]
,x.[Adres]
,x.[Numer]
,x.[Mail]
,x.[Powód]
FROM
(select Notes as [Adres] from CaseActionHistory where CaseActionDefinitionId = 68
UNION ALL
select Info as [Numer] from CaseActionHistory where CaseActionDefinitionId in (54,55,56,58,59,60,61,62,63)
UNION ALL
select Notes as [Mail] from CaseActionHistory where CaseActionDefinitionId = 66
UNION ALL
select Description as [Powód] from CaseActionDefinition join CaseActionHistory as C on DefinitionId = C.CaseActionDefinitionId where DefinitionId between 70 and 78)
AS x
inner join CaseDetails as C on x.CaseDetailId = C.CaseDetaislId
inner join CaseActionHistory as CA on C.CaseDetailsId = CA.CaseDetailId
where C.ClientId = '11'
GROUP by C.ClientCaseNumber