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

我想要在单个查询中将分组列截断到当天,并添加 11 个附加列来表示和create_date之间的差异,因此结果大致如下所示:processing_datecreate_date

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

等等,我希望你明白这一点:)

我已经尝试过了,到目前为止,它可以为 a 获取一个聚合列,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 的了解不够。

我需要的是一个提示,以便我可以将各种差异包含在列中,如上所示。我怎么可能做到这一点?

4

3 回答 3

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')
于 2012-10-10T19:42:50.287 回答
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
于 2012-10-10T19:44:54.010 回答
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 
于 2012-10-10T20:46:56.310 回答