Oracle:在一个查询中总结这个和这个

时间:2013-01-30 23:53:11

标签: sql oracle

我订购了包含上周订单的订单表以及交付订单的驱动程序的ID。它看起来有点像这样:

ORDERDATE,    ORDERNO,    DRIVER
23/01/2013,    901398503,    1
23/01/2013,    901332159,    1
23/01/2013,    901334158,    2
24/01/2013,    901338455,    1
25/01/2013,    902907513,    1
25/01/2013,    902338553,    2
25/01/2013,    903936533,    2
27/01/2013,    903944523,    1
27/01/2013,    903981522,    2
27/01/2013,    911334951,    1
28/01/2013,    911338851,    1
28/01/2013,    911339259,    1
28/01/2013,    912332555,    2
28/01/2013,    912336650,    2
29/01/2013,    912337655,    1
29/01/2013,    913969582,    1
29/01/2013,    913973583,    1
29/01/2013,    913982552,    1
29/01/2013,    916379158,    1

我想选择ORDERDATE,ORDERCOUNT,DRIVER_1_COUNT,DRIVER_2_COUNT。

所以,日期|总订单|司机1的总订单司机2的总订单

此外,如果ORDERDATE,ORDERCOUNT,DRIVER_1_COUNT或DRIVER_2_COUNT为0(或为空),我需要零。

(在oracle中)我可以选择上周每天的日期,以及每天的零订单数(占位符),如下所示:

select 
 TRUNC(NEXT_DAY(sysdate,'SUNDAY')-7 +i) ORDERDATE,
 0 as ORDERCOUNT   
from
 (select rownum i from all_objects where rownum < 8)

我应该可以使用此输出来确保最终结果中没有缺少天数(在此示例中没有订单26日)

ORDERDATE,ORDERCOUNT
23/01/2013,0
24/01/2013,0
25/01/2013,0
26/01/2013,0
27/01/2013,0
28/01/2013,0
29/01/2013,0

我需要这个输出:

ORDERDATE,ORDERCOUNT,DRIVER_1_COUNT,DRIVER_2_COUNT
23/01/2013,3,2,1
24/01/2013,1,1,0
25/01/2013,3,1,2
26/01/2013,0,0,0
27/01/2013,3,2,1
28/01/2013,4,2,2
29/01/2013,5,5,0

我可以获得ORDERDATE&amp; ORDERCOUNT(简单求和)并与其他查询联合以避免错过天数,但我也无法弄清楚如何为每个驱动程序求和。

先谢谢你的帮助。

3 个答案:

答案 0 :(得分:1)

在Oracle 11g中,您可以这样做: -

SELECT *
FROM orders
PIVOT (
  COUNT( ORDERNO )
  FOR DRIVER IN (1,2,3)
)

有关详细说明,请参阅pivot and unpivot queries in 11g

答案 1 :(得分:0)

首先,您需要总结和交叉表结果:

SELECT ORDERDATE, SUM(ORDERCOUNT) ORDERCOUNT, 
SUM(DECODE(DRIVER,1,ORDERCOUNT,0)) DRIVER_1_COUNT, 
SUM(DECODE(DRIVER,2,ORDERCOUNT,0)) DRIVER_2_COUNT
FROM (
    SELECT ORDERDATE, DRIVER, COUNT(*) ORDERCOUNT
    FROM YourTable
    GROUP BY ORDERDATE, DRIVER
) S
GROUP BY  ORDERDATE

在Oracle

中可能有更明智的方法可以做到这一点

然后你需要通过外部加入空白来填写空白:

(注意上面的查询在此查询中别名为'T')。

SELECT D.ORDERDATE, 
NVL(T.ORDERCOUNT,0) ORDERCOUNT, 
NVL(T.DRIVER_1_COUNT,0) DRIVER_1_COUNT, 
NVL(T.DRIVER_1_COUNT,0) DRIVER_2_COUNT
FROM 
(
SELECT ORDERDATE, SUM(ORDERCOUNT) ORDERCOUNT, 
SUM(DECODE(DRIVER,1,ORDERCOUNT,0)) DRIVER_1_COUNT, 
SUM(DECODE(DRIVER,2,ORDERCOUNT,0)) DRIVER_2_COUNT
FROM 
    (
    SELECT ORDERDATE, DRIVER, COUNT(*) ORDERCOUNT
    FROM YourTable
    GROUP BY ORDERDATE, DRIVER
    ) S
GROUP BY  ORDERDATE
) T
RIGHT OUTER JOIN
(
SELECT
TRUNC(NEXT_DAY(sysdate,'SUNDAY')-7 +i) ORDERDATE
FROM (select rownum i from all_objects where rownum < 8)
) D
ON D.ORDERDATE = T.ORDERDATE

答案 2 :(得分:0)

您必须从子查询中进行选择。这样的事情应该有效。

select orderdate, ordercount, sum(driver1) driver1count, sum(driver2) driver2count
from (
select orderdate
, case when driver = 1 then 1 else 0 end driver1
, case when driver = 2 then 1 else 0 end driver2
, count(*) ordercount

from yourtable

where whatever

group by orderdate
, case when driver = 1 then 1 else 0 end driver1
, case when driver = 2 then 1 else 0 end driver2

) you_need_an_alias_here

group by orderdate, ordercount

order by orderdate