按周分组日期:打开,关闭,有效

时间:2012-10-02 12:35:39

标签: sql

这是我的情景。我有一张这样的桌子:

------------------------------------------------------------------
|  ticket     |     start date      |    finish date             |
------------------------------------------------------------------
|   123       |     1 apr 12        |        20 apr 12           |
|   124       |     4 apr 12        |        28 apr 12           |
|   125       |    16 apr 12        |         NULL               |
|   126       |    28 apr 12        |        4 may 12            |
|   127       |     2 may 12        |         NULL               |
------------------------------------------------------------------

我需要得到这样的结果集:

------------------------------------------------------------------
|  week       |     opened      |    closed       |    active    |
------------------------------------------------------------------
|   5         |       3         |        2        |     50       |
|   6         |       4         |        5        |     49       |
|   7         |       2         |        6        |     45       |
|   8         |       5         |        4        |     46       |
------------------------------------------------------------------

基本上,我想知道在给定的一周内打开了多少张门票,关闭了多少张门票,以及在那一周内有多少门票活跃(之前已开放,尚未关闭)。

我想我可能已经想出了如何派生开放和关闭,但我真的无法查询活动列。有什么想法吗?

更新:按要求添加信息。

这适用于SQL。

我到目前为止的查询是这样的,它不包括有效的:

    SELECT
    a.week,
    a.created,
    b.closed
    FROM
    (
    SELECT
        DATEPART(WEEK, t.[start date]) AS week,
        COUNT(t.[ticket]) AS opened
        FROM sqltable AS t
        WHERE [start date] > GETDATE() - 69 -- last 10 weeks
        GROUP BY
        DATEPART(WEEK, t.[start date])
    ) AS a
    LEFT JOIN
    (
        SELECT
        DATEPART(WEEK, t.[finish date]) AS week,
        COUNT(t.[ticket]) AS closed
        FROM sqltable AS t
        WHERE [finish date] > GETDATE() - 69 -- last 10 weeks
        GROUP BY
        DATEPART(WEEK, t.[finish date])
    ) AS b
    ON a.week = b.week
    ORDER BY a.week;

2 个答案:

答案 0 :(得分:1)

在任何数据库中,您都可以使用相关子查询执行此操作。但是,日期函数在数据库中并不相同,因此我假设您知道如何执行此操作。

select weeknum, sum(startweek) as starts, sum(endweek) as ends,
       (select count(*) as numstarts
        from t ts
        where DATEPART(WEEK, ts.[start date]) <= weeknum and
              datepart(week, ts.[end date]) >= weeknum
       ) as actives
from ((select DATEPART(WEEK, t.[start date]) AS weeknum, 1 as startweek, 0 endweek,
              t.[start date] as startdate, NULL as enddate
       from t
      ) union all
      (select DATEPART(WEEK, t.[end date]) AS weeknum, 0 as startweek, 1 as endweek,
              NULL, t.[end date] as enddate
       from t
      )
     ) t
group by weeknum
order by 1

如果您使用的是SQL Server 2012(或Oracle),那么您也可以使用累积总和来执行此操作。

答案 1 :(得分:0)

使用IF语句可以轻松实现此目的,并为累积和的每个真实结果生成+1:

SELECT
  DATE_FORMAT(startdate, "%U") as week,
  SUM(IF(startdate<>'', 1, 0)) as opened,
  SUM(IF(enddate<>'', 1, 0)) as closed,
  SUM(IF(startdate<>'' AND enddate<>'',  0, 1)) as active
FROM ticket
GROUP BY week;

查询很简单,希望它有帮助

在MySQL 5.6中尝试了SQLFiddle