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), () );
Related
I m having data in columns as:
item_id
month_in
amount
1
1
1500
1
1
1000
2
1
2500
3
1
2600
3
1
1000
4
1
2700
4
1
1000
1
2
1500
1
2
2000
2
2
1000
3
3
2500
3
3
2500
4
3
1000
4
3
2500
I want to have like this result
item_id
januari
februari
maret
1
2500
3500
0
2
2500
1000
0
3
3600
0
0
4
3700
0
3500
in oracle sql query how to solve this.
please help me
I have try this
select
item_id,
(case month_in=1 then sum(amout) end) AS januari
from table
group by item_id, month_in
order by item_id asc
but not working as I expected
We can try a pivot query here:
SELECT
item_id,
SUM(CASE WHEN month_in = 1 THEN amount ELSE 0 END) AS januari,
SUM(CASE WHEN month_in = 2 THEN amount ELSE 0 END) AS februari,
SUM(CASE WHEN month_in = 3 THEN amount ELSE 0 END) AS maret
FROM yourTable
GROUP BY
item_id
ORDER BY
item_id;
You almost got it. This is the simplest solution, without extra hassle:
SELECT
item_id,
SUM(CASE WHEN month_in = 1 THEN amount ELSE 0 END) AS januari,
SUM(CASE WHEN month_in = 2 THEN amount ELSE 0 END) AS februari,
SUM(CASE WHEN month_in = 3 THEN amount ELSE 0 END) AS maret
from table
group by item_id
order by item_id asc
You don't need to define the months as a separate rowset, because the months are defined as values 1, 2, 3, ... as columns. Similarly, the items are defined by the group by function.
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
I'm using the following SQL request to Informix DB:
select fromQ, toQ, count(callid) as cont_num, type
from some_table
group by fromQ, toQ, type
order by fromQ, toQ;
It produces the result:
fromq toq cont_num type
-----------------------------------
Sales 12 1
English 1 1
MB 59 1
Reception 3 2
Reception 53 1
Service 1 1
MB Sales 1 1
MB English 1 1
This is OK, as expected. Please note there are 2 rows for toq=Reception.
Field WRTYPE can have values only from 1 to 3.
So idea is to make an output like this:
fromq toq cont_num type1 type2 type3
------------------------------------------------
Sales 12 12 0 0
English 1 1 0 0
MB 59 59 0 0
Reception 56 53 3 0
Service 1 1 0 0
MB Sales 1 1 0 0
MB English 1 1 0 0
Is there a simple way to do this?
Use conditional aggregation:
select fromQ, toQ, count(callid) as cont_num,
sum(case when type = 1 then 1 else 0 end) as type_1,
sum(case when type = 2 then 1 else 0 end) as type_2,
sum(case when type = 3 then 1 else 0 end) as type_3
from some_table
group by fromQ, toQ
order by fromQ, toQ;
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 table:
IdSce Year NoIte Value
1 0 1 1
1 0 2 5
1 0 3 1
1 1 1 2
1 1 2 3
1 1 3 2
2 0 1 4
2 0 2 4
2 0 3 1
2 1 1 2
2 1 2 4
2 1 3 3
I want to group by IdSce and Year, and show each possible value and count how many time each value appears like this:
IdSce Year Value1 Value2 Value3 Value4 Value5
1 0 2 0 0 0 1
1 1 0 2 1 0 0
2 0 1 0 0 2 0
2 1 0 1 1 1 0
Thanks !
EDIT
shawnt00 is really close to what I want, but I'm looking to do it as dynamic as possible, meaning if I have 10 different values for the column value, I will be missing information in my table. Therefore, if I have 10 different values, I want 10 new columns (value1, value2, ... , value10)
This is what I've tried so far:
SELECT IdSce
,Year
,SUM(CASE WHEN Value >= 0 and Value < 1 THEN 1 else 0 end) Zero
,SUM(CASE WHEN Value >= 1 and Value < 2 THEN 1 else 0 end) One
,SUM(CASE WHEN Value >= 2 and Value < 3 THEN 1 else 0 end) Two
,SUM(CASE WHEN Value >= 3 and Value < 4 THEN 1 else 0 end) Three
,SUM(CASE WHEN Value >= 4 and Value < 5 THEN 1 else 0 end) Four
,SUM(CASE WHEN Value >= 5 THEN 1 else 0 end) FiveMore
,SUM(CASE WHEN Value >= 0 THEN 1 else 0 end) Total
FROM Table
GROUP BY IdSce
,Year
Thanks for the help again!
Ok, I'll do it!
select IdSce, "Year"
count(case when Value = 1 then 1 end) as "1",
count(case when Value = 2 then 1 end) as "2",
count(case when Value = 3 then 1 end) as "3",
count(case when Value = 4 then 1 end) as "4",
count(case when Value = 5 then 1 end) as "5"
from T
group by IdSce, "Year"
I think you'll often find this filed under "conditional aggregation". SQL Server has a proprietary syntax that uses pivot if you want to look into that also.