Sql 选择“打开”、“关闭”和“积压”

Sql 选择“打开”、“关闭”和“积压”,sql,sql-server,select,group-by,Sql,Sql Server,Select,Group By,我有下表: +-------------+---------------------+---------------------+ | status | open_time | close_time | +-------------+---------------------+---------------------+ | closed | 01-11-2014 19:32:44 | 01-11-2014 20:32:44 | |

我有下表:

+-------------+---------------------+---------------------+
| status      | open_time           | close_time          |
+-------------+---------------------+---------------------+
|      closed | 01-11-2014 19:32:44 | 01-11-2014 20:32:44 |
|        open | 01-12-2014 22:33:49 | 02-12-2014 22:33:49 |
|        open | 01-23-2014 22:08:24 | 03-23-2014 22:08:24 |
|      closed | 02-01-2014 22:33:57 | 03-01-2014 22:33:57 |
|        open | 02-01-2013 22:37:34 | 02-01-2013 23:37:34 |
|      closed | 04-20-2013 15:23:00 | 05-20-2013 15:23:00 |
|        open | 04-20-2013 12:21:49 | 05-20-2013 12:21:49 |
|      closed | 04-25-2013 11:22:00 | 06-25-2013 11:22:00 |
|      closed | 05-20-2013 14:23:49 | 10-20-2013 14:23:49 |
|      closed | 04-20-2013 16:33:49 | 04-25-2013 16:33:49 |
+-------------+---------------------+---------------------*
我想展示一下这个结果:

+-------------+---------------+--------------+---------+
| Year | Month | Opened Cases | Closed Cases | Backlog |
+-------------+---------------+--------------+---------+
| 2014 |     4 |           10 |            5 |      62 | (57 + 5)
| 2014 |     3 |            9 |            7 |      57 | (52 + 2)    
| 2014 |     2 |           15 |           20 |      52 | (57 - 5)
| 2014 |     1 |           12 |            1 |      57 | (46 + 11)
| 2013 |    12 |           10 |            9 |      46 | (45 + 1)
| 2013 |    11 |           50 |            5 |      45 | (45)
+--------------+--------------+--------------+---------+
实际上我面临两种情况:

情况1:我无法为已关闭的案例获取正确的值。尝试在where中使用子句,但没有成功

情况2:在重新审理已结案的案件后,我应该进行开封-结案,并在几个月内累积,因此这将是积压

对于情况1: 如果我执行以下操作,请选择:

SELECT
  YEAR(open_time) AS Ano,
  MONTH(open_time) AS Mes,
  sum(CASE WHEN DATEPART(YYYY, open_time)= 2013 AND DATEPART(MM, open_time)= 11 THEN 1 ELSE 0 END) Abertos,
  sum(CASE WHEN DATEPART(YYYY, close_time)= 2013 AND DATEPART(MM, close_time)= 11 THEN 1 ELSE 0 END) Fechados
FROM
  TABLE
WHERE
  GROUPDESC= 'SUPPORT'
GROUP BY
  MONTH(open_time),
  YEAR(open_time)
ORDER BY
  Ano DESC,
  Mes DESC
我发现这是闭合案例的正确值:

+-------------+---------------+--------------+
| Year | Month | Opened Cases | Closed Cases |
+-------------+---------------+--------------+
| 2014 |     4 |            0 |            0 |
| 2014 |     3 |            0 |            0 |
| 2014 |     2 |            0 |            0 |
| 2014 |     1 |            0 |            0 |
| 2013 |    12 |            0 |            0 |
| 2013 |    11 |           50 |            5 |
+--------------+--------------+--------------+
但如果我这样做:

SELECT
  YEAR(open_time) AS Ano,
  MONTH(open_time) AS Mes,
  sum(CASE WHEN DATEPART(YYYY, open_time)= YEAR(open_time) AND DATEPART(MM, open_time)= MONTH(open_time) THEN 1 ELSE 0 END) Abertos,
  sum(CASE WHEN DATEPART(YYYY, close_time)= YEAR(close_time) AND DATEPART(MM, close_time)= YEAR(close_time) THEN 1 ELSE 0 END) Fechados
FROM
  TABLE
WHERE
  GROUPDESC= 'SUPPORT'
GROUP BY
  MONTH(open_time),
  YEAR(open_time)
ORDER BY
  Ano DESC,
  Mes DESC
我得到:

+-------------+---------------+--------------+
| Year | Month | Opened Cases | Closed Cases |
+-------------+---------------+--------------+
| 2014 |     4 |            0 |            0 |
| 2014 |     3 |            0 |            0 |
| 2014 |     2 |            0 |            0 |
| 2014 |     1 |            0 |            0 |
| 2013 |    12 |            0 |            0 |
| 2013 |    11 |           50 |           50 |
+--------------+--------------+--------------+
试试这个

;with CTE as (
    SELECT
        row_number() over (partition by groupdesc order by YEAR(open_time), MONTH(open_time)) "Date_Order",
        YEAR(open_time) "Year",
        MONTH(open_time) "Month",
        SUM(CASE status WHEN 'open' THEN 1 ELSE 0 END) "open_count",
        SUM(CASE status WHEN 'closed' THEN 1 ELSE 0 END) "closed_count",
        SUM(CASE status WHEN 'open' THEN 1 ELSE 0 END) - SUM(CASE status WHEN 'closed' THEN 1 ELSE 0 END) "monthly_change"
    FROM PROBSUMMARYM1
    WHERE groupdesc = 'SUPPORT'
    GROUP BY groupdesc,
        YEAR(open_time),
        MONTH(open_time)
)
select c1.Date_Order,
    c1.Year,
    c1.Month,
    c1.open_count,
    c1.closed_count,
    sum(c2.monthly_change) "Backlog"
from CTE c1
join CTE c2
    on c2.Date_Order <= c1.Date_Order
group by c1.Date_Order,
    c1.Year,
    c1.Month,
    c1.open_count,
    c1.closed_count
order by c1.Year desc,
    c1.Month desc;
严格来说,按groupdesc分区然后按groupdesc分组并不是必需的,但它演示了如何使用row_number同时运行一个查询来对多个groupdesc执行相同的操作


您可能可以不使用SUM OVER,但我对OVER子句没有太多经验。具体参见示例C.

这里有一种解决问题的方法:选择每月所有期初计数和每月所有期末计数。然后把它们全部连接起来。累积是一个所谓的总运行,你得到的总和超过

select
  coalesce(opened.ano, closed.ano) as ano,
  coalesce(opened.mes, closed.mes) as mes,
  coalesce(opened.cnt, 0) as opened_cases,
  coalesce(closed.cnt, 0) as closed_cases,
  sum(coalesce(opened.cnt, 0) - coalesce(closed.cnt, 0)) over (order by coalesce(opened.ano, closed.ano), coalesce(opened.mes, closed.mes)) as backlog
from
(
  select 
    year(open_time) as ano, 
    month(open_time) as mes,
    count(*) as cnt
  from probsummarym1
  where groupdesc = 'SUPPORT'
  group by year(open_time), month(open_time)
) opened
full outer join
(
  select 
    year(close_time) as ano, 
    month(close_time) as mes,
    count(*) as cnt
  from probsummarym1
  where groupdesc = 'SUPPORT'
  and status = 'closed'
  group by year(close_time), month(close_time)
) closed
  on opened.ano = closed.ano and opened.mes = closed.mes
order by coalesce(opened.ano, closed.ano) desc, coalesce(opened.mes, closed.mes) desc;
下面是SQL提琴:

另一种方法是使用UNION ALL将开始事件与结束事件粘合在一起,然后计数:

select 
  ano,
  mes,
  opened_cases,
  closed_cases,
  sum(opened_cases - closed_cases) over (order by ano, mes) as backlog
from
(
  select 
    year(fecha) as ano,
    month(fecha) as mes,
    sum(case when evento = 'opened' then 1 else 0 end) as opened_cases,
    sum(case when evento = 'closed' then 1 else 0 end) as closed_cases
  from
  (
    select 'opened' as evento, open_time as fecha
    from probsummarym1
    where groupdesc = 'SUPPORT'
    union all
    select 'closed' as evento, close_time as fecha
    from probsummarym1
    where groupdesc = 'SUPPORT'
    and status = 'closed'
  ) x 
  group by year(fecha), month(fecha)
) y
order by ano desc, mes desc;
下面是第二个SQL问题:

编辑:没有总和吗?那太糟糕了。因此,您必须再选择计数。这很慢,因为每个月都必须再次扫描该表

对于每个月,我们必须找到所有的开始日期,直到那时和所有的结束日期。由于结束永远不会发生在开始之前,因此我们可以选择开始日期匹配的所有记录并对其进行计数。在这些记录中,我们还将找到所有可能的结束日期。我们用求和和和,减法来计算,我们就完成了

因此,您必须将总计替换为积压工作的一部分,因此:

  (
    select
      count(*)
      -
      sum
      (
        case 
          when eventsuntil.status = 'closed' 
          and year(eventsuntil.close_time) * 100 + month(eventsuntil.close_time) >= 
              y.ano * 100 + y.mes 
        then 1 else 0
        end
      )    
    from probsummarym1 eventsuntil
    where eventsuntil.groupdesc = 'SUPPORT'
    and year(eventsuntil.open_time) * 100 + month(eventsuntil.open_time) >= 
        y.ano * 100 + y.mes
  ) as backlog

但是,即使在版本2012中,SQL Server也不能执行这一点,我认为这是DBMS缺陷。y、 对于内部查询,第一条语句的ano和y.mes或coalesceopen.ano、closed.ano和coalesceopen.mes、closed.mes应视为常量,因为计算是根据外部记录(即月份)进行的,但不是。我不知道如何克服这个问题。也许SQL Server方面的专家可以在这里帮助您


下面是导致语法错误的两个小提琴:和。对不起,我不能再帮你了

open_count列仅显示所有行的0。请参阅:是否可以使用日期而不是状态来求和?状态中实际存储了什么?它是真正的“开放”还是其他价值?是否存在应“打开”修剪的空白填充?您是否使用区分大小写的排序规则?原因是我具有“其他人”状态。对不起,我不应该在示例中显示该列,这就是我在示例中使用日期的原因,对不起。是否可以使用日期来求和而不是状态?大概这大大使逻辑复杂化了。我不知道如何使用这样的聚合数据。我可能会创建一个子查询,只计算打开的票证和一个查询,只计算关闭的票证。然后将它们连接起来并使用ROW_NUMBER函数。然后在第三个查询中进行积压工作计算。首先感谢您的时间。这两个例子正是我所需要的,并且运行良好,但在我的场景中,我得到了一个错误,我认为这是因为sql server版本。我的是2008年10月50日。在第一个示例中,除了以下行之外,所有的运行都正常:sumcalesceopen.cnt,0-coalesceclosed.cnt,0 over order by coalesceceopen.ano,closed.ano,coalesceceopen.mes,closed.mes作为backlog。好的,我已经编辑了我的答案。然而,dbms甚至在这种旧式的解决方案中存在问题,因此您需要有关SQL Server的专家建议,而我无法给出。抱歉,祝您好运。第2014.3行的“期望结果”表的“积压工作”列显示57。它应该是54=52+2。