2012-10-10 68 views
2

我被看似簡單的查詢卡住了,但無法設法讓它在最後幾小時工作。將總值的部分計算爲每列的列數(數據透視表)

我有一個表files保存的文件名,並想在這個文件中記錄了一些值,創建(create_date)的日期,處理(processing_date)的日期等。創建日期在不同時間可能有多個文件,很可能它們不會在創建者的同一天處理,事實上,甚至可能需要三天或更長時間才能處理。

所以,讓我們假設我有這行,作爲一個例子:

create_date   | processing_date 
------------------------------ 
2012-09-10 11:10:55.0 | 2012-09-11 18:00:18.0 
2012-09-10 15:20:18.0 | 2012-09-11 13:38:19.0 
2012-09-10 19:30:48.0 | 2012-09-12 10:59:00.0 
2012-09-11 08:19:11.0 | 2012-09-11 18:14:44.0 
2012-09-11 22:31:42.0 | 2012-09-21 03:51:09.0 

我在一個查詢想要什麼是讓一個分組列截斷爲create_date的一天增加11列的差異在processing_datecreate_date之間,這樣的結果應該大致是這樣的:

create_date | diff0days | diff1days | diff2days | ... | diff10days 
------------------------------------------------------------------------ 
2012-09-10 |  0    2   1  ...  0 
2012-09-11 |  1    0   0  ...  1 

等等,我希望你明白了吧:)

我已經試過這一點,到目前爲止,它的作品獲得了create_date單條聚集柱的差異 - 例如 - 3:

SELECT TRUNC(f.create_date, 'DD') as created, count(1) FROM files f WHERE TRUNC(f.process_date, 'DD') - trunc(f.create_date, 'DD') = 3 GROUP BY TRUNC(f.create_date, 'DD') 

我試圖結合單查詢,我試圖子查詢,但這並沒有幫助,或者至少我對SQL的瞭解還不夠。

我需要的是一個提示,以便我可以將各種差異包括爲列,如上所示。 我怎麼可能做到這一點?

回答

2

這基本上樞問題:

SELECT TRUNC(f.create_date, 'DD') as created 
,  sum(case TRUNC(f.process_date, 'DD') - trunc(f.create_date, 'DD') 
        when 0 then 1 end) as diff0days 
,  sum(case TRUNC(f.process_date, 'DD') - trunc(f.create_date, 'DD') 
        when 1 then 1 end) as diff1days 
,  sum(case TRUNC(f.process_date, 'DD') - trunc(f.create_date, 'DD') 
        when 2 then 1 end) as diff2days 
,  ... 
FROM files f 
GROUP BY 
     TRUNC(f.create_date, 'DD') 
+0

你的回答適合我的需要最好的,我補充說: 「否則爲0」 的情況下不能爲NULL。謝謝! – devrys

1
SELECT CreateDate, 
    sum(CASE WHEN DateDiff(day, CreateDate, ProcessDate) = 1 THEN 1 ELSE 0 END) AS Diff1, 
    sum(CASE WHEN DateDiff(day, CreateDate, ProcessDate) = 2 THEN 1 ELSE 0 END) AS Diff2, 
    ... 
FROM table 
GROUP BY CreateDate 
ORDER BY CreateDate 
+0

我不認爲Oracle有'DateDiff'函數,但是這可以在SQL Server中工作 – Andomar

+0

的確如此,DateDiff不可用,但是可以簡單地減去DATE,因此基本上是相同的。謝謝! – devrys

1

當你使用的是Oracle 11g中,您還可以通過使用pivot query得到期望的結果。

下面是一個例子:

-- sample of data from your question 
    SQL> create table Your_table(create_date, processing_date) as 
    2 (
    3  select '2012-09-10', '2012-09-11' from dual union all 
    4  select '2012-09-10', '2012-09-11' from dual union all 
    5  select '2012-09-10', '2012-09-12' from dual union all 
    6  select '2012-09-11', '2012-09-11' from dual union all 
    7  select '2012-09-11', '2012-09-21' from dual 
    8 ) 
    9 ; 

Table created 



SQL> with t2 as(
    2 select create_date 
    3   , processing_date 
    4   , to_date(processing_date, 'YYYY-MM-DD') 
         - To_Date(create_date, 'YYYY-MM-DD') dif 
    5  from your_table 
    6 ) 
    7  select create_date 
    8   , max(diff0) diff0 
    9   , max(diff1) diff1 
10   , max(diff2) diff2 
11   , max(diff3) diff3 
12   , max(diff4) diff4 
13   , max(diff5) diff5 
14   , max(diff6) diff6 
15   , max(diff7) diff7 
16   , max(diff8) diff8 
17   , max(diff9) diff9 
18   , max(diff10) diff10 
19  from (select * 
20    from t2 
21    pivot(
22      count(dif) 
23      for dif in (0 diff0 
24         , 1 diff1 
25         , 2 diff2 
26         , 3 diff3 
27         , 4 diff4 
28         , 5 diff5 
29         , 6 diff6 
30         , 7 diff7 
31         , 8 diff8 
32         , 9 diff9 
33         , 10 diff10 
34         ) 
35     ) pd 
36   ) res 
37  group by create_date 
38 ; 

結果:

Create_Date Diff0 Diff1 Diff2 Diff3 Diff4 Diff5 Diff6 Diff7 Diff8 Diff9 Diff10 
-------------------------------------------------------------------------------- 
2012-09-10 0  2  1  0  0  0  0 0  0  0  0 
2012-09-11 1  0  0  0  0  0  0 0  0  0  1 
+0

這看起來不錯,我試了一下,它的工作原理。但那是硬編碼的日期,我想在相應的行中使用日期。這也可以用數據透視查詢嗎?謝謝! – devrys

+1

我已經更新了答案。希望現在看起來更好。 :) –

+0

不錯,我會試試看:) – devrys