Reputation: 37
I can't figure this out, and I've been trying and looking around the internet for 3, 4 hours now. I'm trying to write a SQL query inside a JAVA project, and I really can't find a solution that suits my needs.
Here is my (probably uselessly long) SQL query :
SELECT count(*), center, achiev_dat
FROM CONV_HC.CARE_PLANS
WHERE center = 961 AND ACHIEV_DAT >= '01/01/2018' AND ACHIEV_DAT <= '31/12/2018'
GROUP BY achiev_dat, center
UNION
SELECT count(*), center, achiev_dat
FROM CONV_HC.CARE_PLANS
WHERE center = 931 AND ACHIEV_DAT >= '01/01/2018' AND ACHIEV_DAT <= '31/12/2018'
GROUP BY achiev_dat, center
UNION
SELECT count(*), center, achiev_dat
FROM CONV_HC.CARE_PLANS
WHERE center = 923 AND ACHIEV_DAT >= '01/01/2018' AND ACHIEV_DAT <= '31/12/2018'
GROUP BY achiev_dat, center
UNION
SELECT count(*), center, achiev_dat
FROM CONV_HC.CARE_PLANS
WHERE center = 913 AND ACHIEV_DAT >= '01/01/2018' AND ACHIEV_DAT <= '31/12/2018'
GROUP BY achiev_dat, center
UNION
SELECT count(*), center, achiev_dat
FROM CONV_HC.CARE_PLANS
WHERE center = 902 AND ACHIEV_DAT >= '01/01/2018' AND ACHIEV_DAT <= '31/12/2018'
GROUP BY achiev_dat, center
Here's the output :
[COUNT(*), CENTER, DATE]
[420, 931, 2018-04-30 00:00:00.0]
[421, 923, 2018-02-23 00:00:00.0]
[421, 961, 2018-05-06 00:00:00.0]
[422, 923, 2018-03-19 00:00:00.0]
[422, 923, 2018-03-20 00:00:00.0]
[422, 923, 2018-04-05 00:00:00.0]
[423, 961, 2018-04-22 00:00:00.0]
[424, 923, 2018-04-03 00:00:00.0]
... And so on...
But I'm looking for a result that would look like this :
[CENTER, JANUARY, FEBRUARY, MARCH, ...]
[961, january count, february count, march count, ...]
[931, january count, february count, march count, ...]
[923, january count, february count, march count, ...]
[902, january count, february count, march count, ...]
[913, january count, february count, march count, ...]
[ALL, january count, february count, march count, ...]
The CENTER parameter has 5 variables : 902, 913, 923, 931 and 961. I believe I have to PIVOT the months, but I first need to group them so results between 2018-01-01 and 2018-01-31 are January, between 2018-02-01 and 2018-02-28 are grouped under February, ... I tried :
ORDER BY CASE WHEN achiev_dat BETWEEN TO_DATE('2018-01-01', 'YYYY-MM-DD')
AND TO_DATE('2018-05-31', 'YYYY-MM-DD') THEN 'January'
But it doesn't do anything. Let alone merging this with a PIVOT function. It's beyond my SQL knowledges.
Thanks a lot for any help !
Upvotes: 2
Views: 78
Reputation: 1271191
You can do this using conditional aggregation:
SELECT COALESCE(TO_CHAR(center), 'Total') as center,
SUM(CASE WHEN TO_CHAR(ACHIEV_DAT, 'MM') = '01' THEN 1 ELSE 0 END) as jan,
SUM(CASE WHEN TO_CHAR(ACHIEV_DAT, 'MM') = '02' THEN 1 ELSE 0 END) as feb,
. . .
FROM CONV_HC.CARE_PLANS
WHERE center IN (961, 931, . . . ) AND
ACHIEV_DAT >= '2018-01-01' AND
ACHIEV_DAT <= '2018-12-31'
GROUP BY ROLLUP(center)
Upvotes: 1
Reputation: 168796
Oracle 11g R2 Schema Setup:
CREATE TABLE CONV_HC.care_plans ( center, achiev_dat ) AS
SELECT 961, DATE '2018-01-01' FROM DUAL UNION ALL
SELECT 961, DATE '2018-03-01' FROM DUAL UNION ALL
SELECT 931, DATE '2018-01-01' FROM DUAL;
Query 1:
SELECT COALESCE( TO_CHAR( Center ), 'ALL' ) AS center,
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
FROM (
SELECT COUNT(*) AS cnt,
center,
EXTRACT( MONTH FROM achiev_dat ) AS month
FROM CONV_HC.CARE_PLANS
WHERE center IN ( 961, 931, 923, 913, 902 )
AND ACHIEV_DAT >= DATE '2018-01-01'
AND ACHIEV_DAT < DATE '2019-01-01'
GROUP BY ROLLUP( Center ),
EXTRACT( MONTH FROM achiev_dat )
)
PIVOT ( MAX(cnt) FOR month IN (
1 AS January,
2 AS February,
3 AS March,
4 AS April,
5 AS May,
6 AS June,
7 AS July,
8 AS August,
9 AS September,
10 AS October,
11 AS November,
12 AS December
) )
ORDER BY center NULLS LAST
| CENTER | JANUARY | FEBRUARY | MARCH | APRIL | MAY | JUNE | JULY | AUGUST | SEPTEMBER | OCTOBER | NOVEMBER | DECEMBER |
|--------|---------|----------|--------|--------|--------|--------|--------|--------|-----------|---------|----------|----------|
| 931 | 1 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
| 961 | 1 | (null) | 1 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
| ALL | 2 | (null) | 1 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
Upvotes: 1