我试图按来源分组,然后按日期分组,然后按状态分组,并为每个分组。这在我的代码中有效:
SELECT sourceCode as "Source", to_char(myTimestamp, 'YYYY-MM-DD') as "Date", statusCode as "Status", count(*) as "Count"
FROM archive_table
WHERE myTimestamp BETWEEN TO_TIMESTAMP('2013-09-30','yyyy-mm-dd') AND TO_TIMESTAMP('2013-10-05','yyyy-mm-dd')
GROUP BY sourceCode, to_char(myTimestamp, 'YYYY-MM-DD'), statusCode
ORDER BY 1, 2, 3
这是输出:
Source Date Status Count
Source1 9/30/2013 C 10
Source1 10/1/2013 C 8
Source1 10/2/2013 C 24
Source1 10/2/2013 O 4
Source1 10/3/2013 C 19
Source1 10/3/2013 O 6
Source1 10/4/2013 C 5
Source1 10/4/2013 O 8
Source2 10/4/2013 C 22
Source2 10/4/2013 O 7
Source3 10/1/2013 C 2
Source4 9/30/2013 C 15
Source4 9/30/2013 O 15
Source4 10/1/2013 C 24
Source4 10/1/2013 O 12
Source4 10/2/2013 C 18
Source4 10/2/2013 O 8
Source4 10/3/2013 C 23
Source4 10/3/2013 O 13
但是,我想获得每个来源、每个日期、每个状态的 SUM 或总数。我在 GROUP BY 的末尾尝试了 WITH ROLLUP,但收到了 Oracle 错误。
我希望输出会给出上述结果和一些额外的计数,如下所示:
Source Date Status Count
Source1 84 --Shows counts for each source
Source2 29
Source3 2
Source4 128
Source1 O 18 --Show counts each source by status
Source2 C 7
Source3 O 2
Source3 C 0
Source4 O 48
Source4 C 80
all src 9/30/2013 O 15 --Show counts each source by date & status
all src 9/30/2013 C 25
all src 10/1/2013 O 12
all src 10/1/2013 C 20
all src 10/2/2013 O 15
all src 10/2/2013 C 10
all src 10/3/2013 O 0
all src 10/3/2013 C 25
all src 10/4/2013 O 15
all src 10/4/2013 C 13
也许这在 ROLLUP 中甚至是不可能的
我不确定这是否是合适的解决方案,我发现它返回的结果我无法阅读:
SELECT sourceCode as "Source", to_char(myTimestamp, 'YYYY-MM-DD') as "Date", statusCode as "Status", count(*) as "Count"
FROM archive_table
WHERE myTimestamp BETWEEN TO_TIMESTAMP('2013-09-30','yyyy-mm-dd') AND TO_TIMESTAMP('2013-10-05','yyyy-mm-dd')
GROUP BY
ROLLUP (sourceCode, to_char(myTimestamp, 'YYYY-MM-DD'), statusCode)
ORDER BY 1, 2, 3
这些是输出结果:
Source Date Status Count
Source1 10/1/2013 8
Source1 10/2/2013 C 24
Source1 10/2/2013 O 4
Source1 10/2/2013 28
Source1 10/3/2013 C 19
Source1 10/3/2013 O 6
Source1 10/3/2013 25
Source1 10/4/2013 C 5
Source1 10/4/2013 O 8
Source1 10/4/2013 13
Source1 84
Source2 10/4/2013 C 22
Source2 10/4/2013 O 7
Source2 10/4/2013 29
Source2 29
Source3 10/1/2013 C 2
Source3 10/1/2013 2
Source3 2
Source4 9/30/2013 C 15
Source4 9/30/2013 O 15
Source4 9/30/2013 30
Source4 10/1/2013 C 24
Source4 10/1/2013 O 12
Source4 10/1/2013 36
Source4 10/2/2013 C 18
Source4 10/2/2013 O 8
Source4 10/2/2013 26
Source4 10/3/2013 C 24
Source4 10/3/2013 O 12
Source4 10/3/2013 36
Source4 10/4/2013 C 30
Source4 10/4/2013 O 11
Source4 10/4/2013 41
Source4 169
284