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

发布于 2024-11-05 16:14:43 字数 885 浏览 2 评论 0原文

我得到一个像这样的表:

    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 时,它才在分组依据中相关,对于日期 2 不为空的记录,无论分组依据的状态如何到此日期2。

所需的结果集如下:

 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 

基本上分组依据必须按日期分组,问题是在某些情况下它针对 Date1 列,而在其他情况下针对 Date2 列。如何才能做到这一点?

I got a table like this:

    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

And several other records, Date 1 is not null but it is only relevant in the group by if the Status is A, for the records where Date2 is not null, regardless of the status the group by should be by this Date2.

The desired result set is as follows:

 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 

Basically the group by must group by date, the problem is that in some cases it is for the Date1 column and in the other case is for the Date2 columns. How can this be accomplished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亣腦蒛氧 2024-11-12 16:14:43

您可以按解码或大小写表达式进行分组。只在 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

You can group by a decode, or case, expression. Only tried in Oracle so not sure if this is portable. With this data:

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

You can do:

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;

Which gives:

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
亢潮 2024-11-12 16:14:43

这是一个典型的 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

但是您需要根据您的数据派生一个包含两列日期的表,以便首先进行联接。

It's a typical PIVOT query:

   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

But you need to derive a table containing the dates from both columns, based on your data, to join against first.

奶茶白久 2024-11-12 16:14:43

也许是这样的:

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

Maybe something like this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文