Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MS SQL统计过去7天内满足与案例相同条件的案例数_Sql_Sql Server_Count_Conditional Statements - Fatal编程技术网

MS SQL统计过去7天内满足与案例相同条件的案例数

MS SQL统计过去7天内满足与案例相同条件的案例数,sql,sql-server,count,conditional-statements,Sql,Sql Server,Count,Conditional Statements,我想在SQL PriorityCountInLast7Days中生成一列,计算优先级与该情况相同的情况的数量。如何在SQL中实现这一点 对于每个Emp1,计算最近7天的优先级=当前日期的优先级,以生成最近7天的优先级计数。大小写只是每行的唯一标识符 +------------+------+----------+--------------------------+ | Date | Emp1 | Priority | PriorityCountinLast7days | +----

我想在SQL PriorityCountInLast7Days中生成一列,计算优先级与该情况相同的情况的数量。如何在SQL中实现这一点

对于每个Emp1,计算最近7天的优先级=当前日期的优先级,以生成最近7天的优先级计数。大小写只是每行的唯一标识符

+------------+------+----------+--------------------------+
|    Date    | Emp1 | Priority | PriorityCountinLast7days |
+------------+------+----------+--------------------------+
| 2018-06-01 | A    |        0 |                        0 |
| 2018-06-03 | A    |        0 |                        1 |
| 2018-06-03 | A    |        0 |                        2 |
| 2018-06-03 | A    |        1 |                        1 |
| 2018-06-03 | A    |        2 |                        1 |
| 2018-06-04 | A    |        0 |                        3 |
| 2018-06-01 | B    |        0 |                        1 |
| 2018-06-02 | B    |        0 |                        2 |
| 2018-06-03 | B    |        0 |                        3 |
+------------+------+----------+--------------------------+

您可以尝试交叉应用一个子查询,该子查询获取过去七天内具有相同优先级的同一员工相对于当前行日期的案例计数

SELECT t1.date,
       t1.emp1,
       t1.[case],
       t1.priority,
       x.c prioritycountinlast7days
       FROM elbat t1
            CROSS APPLY (SELECT count(*) c
                                FROM elbat t2
                                WHERE t2.emp = t1.emp
                                      AND t2.priority = t1.priority
                                      AND t2.date >= dateadd(day, -7, t1.date)
                                      AND t2.date <= t1.date) x;

我还是不知道,现在的案子是否应该算在内。更多的样本数据表明是的,我就是这样写的。如果不应该对其进行计数,则将和t2.case t1.case添加到WHERE子句。

您可以使用子查询重新查询表以获取计数。唯一困难的部分是用于应用计数的过滤器。本例中的输出与问题中的预期输出不同,但乍一看,我不确定在给定算法规则的情况下,您是如何确定预期输出的

DECLARE @temp TABLE (Date date, Emp1 char(1), [Case] char(2), Priority int)
INSERT INTO @temp VALUES
 ('2018-06-01', 'A', 'A1', 0) 
,('2018-06-03', 'A', 'A2', 0)
,('2018-06-03', 'A', 'A3', 0)
,('2018-06-03', 'A', 'A4', 1)
,('2018-06-03', 'A', 'A5', 2)
,('2018-06-04', 'A', 'A6', 0)
,('2018-06-01', 'B', 'B1', 0)
,('2018-06-02', 'B', 'B2', 0)
,('2018-06-03', 'B', 'B3', 0) 

SELECT * 
      ,(SELECT COUNT(*)
          FROM @temp T2
         WHERE T2.Emp1 = T1.Emp1
           AND T2.[Date] <= DATEADD(DAY, 7, T1.[Date])
           AND T2.Priority = T1.Priority
           AND T2.[Case] < T1.[Case]

       ) AS [PriorityCountinLast7days]
  FROM @temp T1

从现在起的最后七天还是从当前行中的日期起的最后七天?案例是否独特?当前行的情况是否也应计算在内?“A4”和“A5”的行表示是,而“A1”、“A2”和“A3”的行表示不是@stickybbit yes case对于每一行都应该是唯一的。您刚刚编辑了问题并删除了[case]字段,您说该字段像主键一样唯一地标识了该行。现在我的回答似乎无关紧要。不确定为什么要删除它,因为现在数据结构甚至不是第一个标准形式。
Date        Emp1    Case    Priority    PriorityCountinLast7days
----------------------------------------------------------------
2018-06-01  A       A1      0           0
2018-06-03  A       A2      0           1
2018-06-03  A       A3      0           2
2018-06-03  A       A4      1           0
2018-06-03  A       A5      2           0
2018-06-04  A       A6      0           3
2018-06-01  B       B1      0           0
2018-06-02  B       B2      0           1
2018-06-03  B       B3      0           2