使用一个SQL查询进行分组和透视

时间:2018-05-29 11:50:06

标签: sql oracle date pivot

我无法解决这个问题,而且我现在正在尝试和浏览互联网3个小时。 我试图在JAVA项目中编写SQL查询,但我真的找不到适合我需求的解决方案。

这是我的(可能无用的长)SQL查询:

df2 = df.drop_duplicates(['Year created', 'Week created', 'SUM_New', 'SUM_Closed'])
print(df2)

这是输出:

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

但我正在寻找一个看起来像这样的结果:

[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...

CENTER参数有5个变量:902,913,923,931和961。 我相信我必须坚持这几个月,但我首先需要对它们进行分组,因此2018-01-01和2018-01-31之间的结果是1月,2018-02-01和2018-02-28之间的结果分组在2月份, ... 我试过了:

[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, ...]

但它没有做任何事情。更不用说将它与PIVOT函数合并了。它超出了我的SQL知识。

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您可以使用条件聚合执行此操作:

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)

答案 1 :(得分:1)

SQL Fiddle

Oracle 11g R2架构设置

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;

查询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

<强> Results

| 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) |