SQL如何按2个不同的日期列对数据进行分组?

时间:2011-05-06 15:21:57

标签: sql oracle pivot aggregate-functions

我有一张这样的桌子:

    Id     Date1          Date2     Status
    ----------------------------------------------
     1    01/01/2010     null         A
     2    04/04/2010      05/14/10    X
     3    01/01/2010     null         A
     4    01/11/2010      01/01/2010  X
     5    01/02/2010     null         A

和其他几个记录一样,日期1不为空,但只有在状态为A的情况下,对于Date2不为空的记录,它才与该组相关,无论该Date2应该是该组的状态。

所需的结果集如下:

 Date             Number of A Status        Number of Date 2 not null statuses
------------------------------------------------------------------------------
 01//01/2010            2                                     1
 01/02/2010             1                                     0
 05/14/2010             0                                     1 

基本上,group by必须按日期分组,问题是在某些情况下它是Date1列,而在另一种情况下是Date2列。如何实现这一目标?

3 个答案:

答案 0 :(得分:2)

您可以通过解码或大小写表达式进行分组。仅在Oracle中尝试过,因此不确定它是否可移植。有了这些数据:

create table t42 as
select 1 id, to_date('01/01/2010') date1, null date2, 'A' status from dual
union select 2, to_date('04/04/2010'), to_date('05/14/2010'), 'X' from dual
union select 3, to_date('01/01/2010'), null, 'A' from dual
union select 4, to_date('01/11/2010'), to_date('01/01/2010'), 'X' from dual
union select 5, to_date('01/02/2010'), null, 'A' from dual
/

select * from t42;

ID                     DATE1                     DATE2                     STATUS
---------------------- ------------------------- ------------------------- ------
1                      01/01/2010                                          A     
2                      04/04/2010                05/14/2010                X     
3                      01/01/2010                                          A     
4                      01/11/2010                01/01/2010                X     
5                      01/02/2010                                          A

你可以这样做:

select case when date2 is null and status = 'A' then date1
        else date2 end as "Date",
    sum(case when status = 'A' then 1 else 0 end) as "Number of A status",
    sum(case when date2 is null then 0 else 1 end) as "Number of Date 2 null"
from t42
group by case when date2 is null and status = 'A' then date1 else date2 end
order by 1;

给出了:

Date                      Number of A status     Number of Date 2 null  
------------------------- ---------------------- ---------------------- 
01/01/2010                2                      1                      
01/02/2010                1                      0                      
05/14/2010                0                      1

答案 1 :(得分:1)

这是一个典型的PIVOT查询:

   SELECT x.date,
          SUM(CASE WHEN 'A' IN (y.status, z.status) THEN 1 ELSE 0 END) AS NumStatusA,
          SUM(CASE 
                WHEN y.date2 IS NOT NULL OR z.date2 IS NOT NULL THEN 1 
                ELSE 0 
              END) AS NumDate2NotNull
     FROM (SELECT a.date1 AS date
             FROM YOUR_TABLE a
           UNION 
           SELECT b.date2 AS date
             FROM YOUR_TABLE b) x
LEFT JOIN YOUR_TABLE y ON y.date1 = x.date
LEFT JOIN YOUR_TABLE z ON z.date1 = x.date
 GROUP BY x.date

但您需要根据您的数据派生一个包含两列日期的表格,以便首先加入。

答案 2 :(得分:0)

也许是这样的:

SELECT DISTINCT Date1 as Date,
(SELECT COUNT(*) FROM MyTable WHERE DATE1=MyT.Date1 AND Status = 'A') NumberAStatus,
(SELECT COUNT(*) FROM MyTable WHERE DATE1=MyT.Date1 AND Date2 is not null) NotNullDate2

FROM MyTable MyT