计算多个列和联接中“空”行的记录

时间:2019-02-25 22:33:23

标签: sql sql-server

我在该网站上进行了很多搜索,试图找到解决问题的方法,但我也发现了类似的问题,但是我没有找到适合我情况的解决方案。

我有一个票务表,像这样(它的数据比这多得多):

TICKET

+---------+--------------+------------+------------+
| ticketid|  report_date |   impact   |    open    |
+---------+--------------+------------+------------+
|       1 |   29/01/2019 |          1 |       true |
|       2 |   29/01/2019 |          2 |       true |
|       3 |   30/01/2019 |          4 |       true |
|       4 |   27/01/2019 |          1 |       true |
|       5 |   29/01/2019 |          1 |       true |
|       6 |   30/01/2019 |          2 |       true |
+---------+--------------+------------+------------+

还有另一个表保存了上表中影响列的可能值:

IMPACT

+---------+
| impact  |
+---------+
|       1 |
|       2 |
|       3 |
|       4 |
+---------+

我的目标是从票证表中提取一个结果集,在该表中我按impactreport_dateopen标志进行分组,并计算每个组中的票证数量。因此,对于上面的示例,我想提取以下结果集。

+--------------+------------+------------+-----------+
|  report_date |   impact   |    open    | tkt_count |
+--------------+------------+------------+-----------+
|   27/01/2019 |          1 |       true |         1 |
|   27/01/2019 |          1 |      false |         0 |
|   27/01/2019 |          2 |       true |         0 |
|   27/01/2019 |          2 |      false |         0 |
|   27/01/2019 |          3 |       true |         0 |
|   27/01/2019 |          3 |      false |         0 |
|   27/01/2019 |          4 |       true |         0 |
|   27/01/2019 |          4 |      false |         0 |
|   29/01/2019 |          1 |       true |         2 |
|   29/01/2019 |          1 |      false |         0 |
|   29/01/2019 |          2 |       true |         1 |
|   29/01/2019 |          2 |      false |         0 |
|   29/01/2019 |          3 |       true |         0 |
|   29/01/2019 |          3 |      false |         0 |
|   29/01/2019 |          4 |       true |         0 |
|   29/01/2019 |          4 |      false |         0 |
|   30/01/2019 |          1 |       true |         0 |
|   30/01/2019 |          1 |      false |         0 |
|   30/01/2019 |          2 |       true |         1 |
|   30/01/2019 |          2 |      false |         0 |
|   30/01/2019 |          3 |       true |         0 |
|   30/01/2019 |          3 |      false |         0 |
|   30/01/2019 |          4 |       true |         1 |
|   30/01/2019 |          4 |      false |         0 |
+--------------+------------+------------+-----------+

这看起来很简单,但是问题出在“零”行上。

对于我在此显示的示例,在给定的日期范围内,没有影响力为3的票证或标志为开放标志的票证。而且我无法提出一个查询来显示所有计数,即使没有行包含某些值。

有人可以帮助我吗?

谢谢。

2 个答案:

答案 0 :(得分:3)

要解决此类问题,一种解决方法是生成一个中间结果集,该结果集包含需要为其计算值的所有记录,然后使用聚合将其与原始数据LEFT JOIN合并。 / p>

SELECT
    dt.report_date,
    i.impact,
    op.[open],
    COUNT(t.report_date) tkt_count
FROM 
    (SELECT DISTINCT report_date FROM ticket) dt
    CROSS JOIN impact i
    CROSS JOIN (SELECT 'true' [open] UNION ALL SELECT 'false') op
    LEFT JOIN ticket t 
        ON  t.report_date = dt.report_date
        AND t.impact = i.impact
        AND t.[open] = op.[open]
GROUP BY
    dt.report_date,
    i.impact,
    op.[open]

此查询生成中间结果集,如下所示:

  • report_date:原始数据(report_date)中所有不同的日期
  • impact:包含表impact
  • open:包含truefalse的固定列表(也可以根据原始数据中的不同值构建,但是值false不可用是您的示例数据)

您可以选择更改上述规则,逻辑应保持不变。例如,如果report_date中有空格,则另一个广泛使用的选项是创建日历表

Demo on DB Fiddle

report_date         | impact | open  | tkt_count
:------------------ | -----: | :---- | --------:
27/01/2019 00:00:00 |      1 | false |         0
27/01/2019 00:00:00 |      1 | true  |         1
27/01/2019 00:00:00 |      2 | false |         0
27/01/2019 00:00:00 |      2 | true  |         0
27/01/2019 00:00:00 |      3 | false |         0
27/01/2019 00:00:00 |      3 | true  |         0
27/01/2019 00:00:00 |      4 | false |         0
27/01/2019 00:00:00 |      4 | true  |         0
29/01/2019 00:00:00 |      1 | false |         0
29/01/2019 00:00:00 |      1 | true  |         2
29/01/2019 00:00:00 |      2 | false |         0
29/01/2019 00:00:00 |      2 | true  |         1
29/01/2019 00:00:00 |      3 | false |         0
29/01/2019 00:00:00 |      3 | true  |         0
29/01/2019 00:00:00 |      4 | false |         0
29/01/2019 00:00:00 |      4 | true  |         0
30/01/2019 00:00:00 |      1 | false |         0
30/01/2019 00:00:00 |      1 | true  |         0
30/01/2019 00:00:00 |      2 | false |         0
30/01/2019 00:00:00 |      2 | true  |         1
30/01/2019 00:00:00 |      3 | false |         0
30/01/2019 00:00:00 |      3 | true  |         0
30/01/2019 00:00:00 |      4 | false |         0
30/01/2019 00:00:00 |      4 | true  |         1

答案 1 :(得分:0)

我按天在开始和结束日历表上查询,然后交叉加入所有可用的影响/公开组合,最后在票证数据中购买了,计算非空匹配数。

DECLARE @Impact TABLE(Impact INT)
INSERT @Impact VALUES(1),(2),(3),(4)

DECLARE @Tickets TABLE(report_date DATETIME, Impact INT, IsOpen BIT)
INSERT @Tickets VALUES
('01/29/2019',1,1),('01/29/2019',2,1),('01/30/2019',3,1),('01/27/2019',4,1),('01/29/2019',5,1),('01/30/2019',6,1)

DECLARE @StartDate DATETIME='01/01/2019'
DECLARE @EndDate DATETIME='02/01/2019'


;WITH AllDates AS
(
  SELECT Date = @StartDate 
  UNION ALL
  SELECT  Date= DATEADD(DAY, 1, Date) FROM AllDates WHERE DATEADD(DAY, 1,Date) <= @EndDate
)
,AllImpacts AS
(
    SELECT DISTINCT Impact,IsOpen = 1 FROM @Impact
    UNION
    SELECT DISTINCT Impact,IsOpen = 0 FROM @Impact
),
AllData AS
(
    SELECT  D.Date,A.impact,A.IsOpen
    FROM AllDates D
    CROSS APPLY AllImpacts A
)

SELECT 
    A.Date,A.Impact,A.IsOpen,
    GroupCount = COUNT(T.Impact) 
FROM 
    AllData A
    LEFT OUTER JOIN @Tickets T ON T.report_date=A.Date AND T.Impact=A.Impact AND T.IsOpen = A.IsOpen
GROUP BY
    A.Date,A.Impact,A.IsOpen
ORDER BY
    A.Date,A.Impact,A.IsOpen
OPTION (MAXRECURSION 0);
GO
Date                | Impact | IsOpen | GroupCount
:------------------ | -----: | -----: | ---------:
01/01/2019 00:00:00 |      1 |      0 |          0
01/01/2019 00:00:00 |      1 |      1 |          0
01/01/2019 00:00:00 |      2 |      0 |          0
01/01/2019 00:00:00 |      2 |      1 |          0
01/01/2019 00:00:00 |      3 |      0 |          0
01/01/2019 00:00:00 |      3 |      1 |          0
01/01/2019 00:00:00 |      4 |      0 |          0
01/01/2019 00:00:00 |      4 |      1 |          0
02/01/2019 00:00:00 |      1 |      0 |          0
02/01/2019 00:00:00 |      1 |      1 |          0
02/01/2019 00:00:00 |      2 |      0 |          0
02/01/2019 00:00:00 |      2 |      1 |          0
02/01/2019 00:00:00 |      3 |      0 |          0
02/01/2019 00:00:00 |      3 |      1 |          0
02/01/2019 00:00:00 |      4 |      0 |          0
02/01/2019 00:00:00 |      4 |      1 |          0
03/01/2019 00:00:00 |      1 |      0 |          0
03/01/2019 00:00:00 |      1 |      1 |          0
03/01/2019 00:00:00 |      2 |      0 |          0
03/01/2019 00:00:00 |      2 |      1 |          0
03/01/2019 00:00:00 |      3 |      0 |          0
03/01/2019 00:00:00 |      3 |      1 |          0
03/01/2019 00:00:00 |      4 |      0 |          0
03/01/2019 00:00:00 |      4 |      1 |          0
04/01/2019 00:00:00 |      1 |      0 |          0
04/01/2019 00:00:00 |      1 |      1 |          0
04/01/2019 00:00:00 |      2 |      0 |          0
04/01/2019 00:00:00 |      2 |      1 |          0
04/01/2019 00:00:00 |      3 |      0 |          0
04/01/2019 00:00:00 |      3 |      1 |          0
04/01/2019 00:00:00 |      4 |      0 |          0
04/01/2019 00:00:00 |      4 |      1 |          0
05/01/2019 00:00:00 |      1 |      0 |          0
05/01/2019 00:00:00 |      1 |      1 |          0
05/01/2019 00:00:00 |      2 |      0 |          0
05/01/2019 00:00:00 |      2 |      1 |          0
05/01/2019 00:00:00 |      3 |      0 |          0
05/01/2019 00:00:00 |      3 |      1 |          0
05/01/2019 00:00:00 |      4 |      0 |          0
05/01/2019 00:00:00 |      4 |      1 |          0
06/01/2019 00:00:00 |      1 |      0 |          0
06/01/2019 00:00:00 |      1 |      1 |          0
06/01/2019 00:00:00 |      2 |      0 |          0
06/01/2019 00:00:00 |      2 |      1 |          0
06/01/2019 00:00:00 |      3 |      0 |          0
06/01/2019 00:00:00 |      3 |      1 |          0
06/01/2019 00:00:00 |      4 |      0 |          0
06/01/2019 00:00:00 |      4 |      1 |          0
07/01/2019 00:00:00 |      1 |      0 |          0
07/01/2019 00:00:00 |      1 |      1 |          0
07/01/2019 00:00:00 |      2 |      0 |          0
07/01/2019 00:00:00 |      2 |      1 |          0
07/01/2019 00:00:00 |      3 |      0 |          0
07/01/2019 00:00:00 |      3 |      1 |          0
07/01/2019 00:00:00 |      4 |      0 |          0
07/01/2019 00:00:00 |      4 |      1 |          0
08/01/2019 00:00:00 |      1 |      0 |          0
08/01/2019 00:00:00 |      1 |      1 |          0
08/01/2019 00:00:00 |      2 |      0 |          0
08/01/2019 00:00:00 |      2 |      1 |          0
08/01/2019 00:00:00 |      3 |      0 |          0
08/01/2019 00:00:00 |      3 |      1 |          0
08/01/2019 00:00:00 |      4 |      0 |          0
08/01/2019 00:00:00 |      4 |      1 |          0
09/01/2019 00:00:00 |      1 |      0 |          0
09/01/2019 00:00:00 |      1 |      1 |          0
09/01/2019 00:00:00 |      2 |      0 |          0
09/01/2019 00:00:00 |      2 |      1 |          0
09/01/2019 00:00:00 |      3 |      0 |          0
09/01/2019 00:00:00 |      3 |      1 |          0
09/01/2019 00:00:00 |      4 |      0 |          0
09/01/2019 00:00:00 |      4 |      1 |          0
10/01/2019 00:00:00 |      1 |      0 |          0
10/01/2019 00:00:00 |      1 |      1 |          0
10/01/2019 00:00:00 |      2 |      0 |          0
10/01/2019 00:00:00 |      2 |      1 |          0
10/01/2019 00:00:00 |      3 |      0 |          0
10/01/2019 00:00:00 |      3 |      1 |          0
10/01/2019 00:00:00 |      4 |      0 |          0
10/01/2019 00:00:00 |      4 |      1 |          0
11/01/2019 00:00:00 |      1 |      0 |          0
11/01/2019 00:00:00 |      1 |      1 |          0
11/01/2019 00:00:00 |      2 |      0 |          0
11/01/2019 00:00:00 |      2 |      1 |          0
11/01/2019 00:00:00 |      3 |      0 |          0
11/01/2019 00:00:00 |      3 |      1 |          0
11/01/2019 00:00:00 |      4 |      0 |          0
11/01/2019 00:00:00 |      4 |      1 |          0
12/01/2019 00:00:00 |      1 |      0 |          0
12/01/2019 00:00:00 |      1 |      1 |          0
12/01/2019 00:00:00 |      2 |      0 |          0
12/01/2019 00:00:00 |      2 |      1 |          0
12/01/2019 00:00:00 |      3 |      0 |          0
12/01/2019 00:00:00 |      3 |      1 |          0
12/01/2019 00:00:00 |      4 |      0 |          0
12/01/2019 00:00:00 |      4 |      1 |          0
13/01/2019 00:00:00 |      1 |      0 |          0
13/01/2019 00:00:00 |      1 |      1 |          0
13/01/2019 00:00:00 |      2 |      0 |          0
13/01/2019 00:00:00 |      2 |      1 |          0
13/01/2019 00:00:00 |      3 |      0 |          0
13/01/2019 00:00:00 |      3 |      1 |          0
13/01/2019 00:00:00 |      4 |      0 |          0
13/01/2019 00:00:00 |      4 |      1 |          0
14/01/2019 00:00:00 |      1 |      0 |          0
14/01/2019 00:00:00 |      1 |      1 |          0
14/01/2019 00:00:00 |      2 |      0 |          0
14/01/2019 00:00:00 |      2 |      1 |          0
14/01/2019 00:00:00 |      3 |      0 |          0
14/01/2019 00:00:00 |      3 |      1 |          0
14/01/2019 00:00:00 |      4 |      0 |          0
14/01/2019 00:00:00 |      4 |      1 |          0
15/01/2019 00:00:00 |      1 |      0 |          0
15/01/2019 00:00:00 |      1 |      1 |          0
15/01/2019 00:00:00 |      2 |      0 |          0
15/01/2019 00:00:00 |      2 |      1 |          0
15/01/2019 00:00:00 |      3 |      0 |          0
15/01/2019 00:00:00 |      3 |      1 |          0
15/01/2019 00:00:00 |      4 |      0 |          0
15/01/2019 00:00:00 |      4 |      1 |          0
16/01/2019 00:00:00 |      1 |      0 |          0
16/01/2019 00:00:00 |      1 |      1 |          0
16/01/2019 00:00:00 |      2 |      0 |          0
16/01/2019 00:00:00 |      2 |      1 |          0
16/01/2019 00:00:00 |      3 |      0 |          0
16/01/2019 00:00:00 |      3 |      1 |          0
16/01/2019 00:00:00 |      4 |      0 |          0
16/01/2019 00:00:00 |      4 |      1 |          0
17/01/2019 00:00:00 |      1 |      0 |          0
17/01/2019 00:00:00 |      1 |      1 |          0
17/01/2019 00:00:00 |      2 |      0 |          0
17/01/2019 00:00:00 |      2 |      1 |          0
17/01/2019 00:00:00 |      3 |      0 |          0
17/01/2019 00:00:00 |      3 |      1 |          0
17/01/2019 00:00:00 |      4 |      0 |          0
17/01/2019 00:00:00 |      4 |      1 |          0
18/01/2019 00:00:00 |      1 |      0 |          0
18/01/2019 00:00:00 |      1 |      1 |          0
18/01/2019 00:00:00 |      2 |      0 |          0
18/01/2019 00:00:00 |      2 |      1 |          0
18/01/2019 00:00:00 |      3 |      0 |          0
18/01/2019 00:00:00 |      3 |      1 |          0
18/01/2019 00:00:00 |      4 |      0 |          0
18/01/2019 00:00:00 |      4 |      1 |          0
19/01/2019 00:00:00 |      1 |      0 |          0
19/01/2019 00:00:00 |      1 |      1 |          0
19/01/2019 00:00:00 |      2 |      0 |          0
19/01/2019 00:00:00 |      2 |      1 |          0
19/01/2019 00:00:00 |      3 |      0 |          0
19/01/2019 00:00:00 |      3 |      1 |          0
19/01/2019 00:00:00 |      4 |      0 |          0
19/01/2019 00:00:00 |      4 |      1 |          0
20/01/2019 00:00:00 |      1 |      0 |          0
20/01/2019 00:00:00 |      1 |      1 |          0
20/01/2019 00:00:00 |      2 |      0 |          0
20/01/2019 00:00:00 |      2 |      1 |          0
20/01/2019 00:00:00 |      3 |      0 |          0
20/01/2019 00:00:00 |      3 |      1 |          0
20/01/2019 00:00:00 |      4 |      0 |          0
20/01/2019 00:00:00 |      4 |      1 |          0
21/01/2019 00:00:00 |      1 |      0 |          0
21/01/2019 00:00:00 |      1 |      1 |          0
21/01/2019 00:00:00 |      2 |      0 |          0
21/01/2019 00:00:00 |      2 |      1 |          0
21/01/2019 00:00:00 |      3 |      0 |          0
21/01/2019 00:00:00 |      3 |      1 |          0
21/01/2019 00:00:00 |      4 |      0 |          0
21/01/2019 00:00:00 |      4 |      1 |          0
22/01/2019 00:00:00 |      1 |      0 |          0
22/01/2019 00:00:00 |      1 |      1 |          0
22/01/2019 00:00:00 |      2 |      0 |          0
22/01/2019 00:00:00 |      2 |      1 |          0
22/01/2019 00:00:00 |      3 |      0 |          0
22/01/2019 00:00:00 |      3 |      1 |          0
22/01/2019 00:00:00 |      4 |      0 |          0
22/01/2019 00:00:00 |      4 |      1 |          0
23/01/2019 00:00:00 |      1 |      0 |          0
23/01/2019 00:00:00 |      1 |      1 |          0
23/01/2019 00:00:00 |      2 |      0 |          0
23/01/2019 00:00:00 |      2 |      1 |          0
23/01/2019 00:00:00 |      3 |      0 |          0
23/01/2019 00:00:00 |      3 |      1 |          0
23/01/2019 00:00:00 |      4 |      0 |          0
23/01/2019 00:00:00 |      4 |      1 |          0
24/01/2019 00:00:00 |      1 |      0 |          0
24/01/2019 00:00:00 |      1 |      1 |          0
24/01/2019 00:00:00 |      2 |      0 |          0
24/01/2019 00:00:00 |      2 |      1 |          0
24/01/2019 00:00:00 |      3 |      0 |          0
24/01/2019 00:00:00 |      3 |      1 |          0
24/01/2019 00:00:00 |      4 |      0 |          0
24/01/2019 00:00:00 |      4 |      1 |          0
25/01/2019 00:00:00 |      1 |      0 |          0
25/01/2019 00:00:00 |      1 |      1 |          0
25/01/2019 00:00:00 |      2 |      0 |          0
25/01/2019 00:00:00 |      2 |      1 |          0
25/01/2019 00:00:00 |      3 |      0 |          0
25/01/2019 00:00:00 |      3 |      1 |          0
25/01/2019 00:00:00 |      4 |      0 |          0
25/01/2019 00:00:00 |      4 |      1 |          0
26/01/2019 00:00:00 |      1 |      0 |          0
26/01/2019 00:00:00 |      1 |      1 |          0
26/01/2019 00:00:00 |      2 |      0 |          0
26/01/2019 00:00:00 |      2 |      1 |          0
26/01/2019 00:00:00 |      3 |      0 |          0
26/01/2019 00:00:00 |      3 |      1 |          0
26/01/2019 00:00:00 |      4 |      0 |          0
26/01/2019 00:00:00 |      4 |      1 |          0
27/01/2019 00:00:00 |      1 |      0 |          0
27/01/2019 00:00:00 |      1 |      1 |          0
27/01/2019 00:00:00 |      2 |      0 |          0
27/01/2019 00:00:00 |      2 |      1 |          0
27/01/2019 00:00:00 |      3 |      0 |          0
27/01/2019 00:00:00 |      3 |      1 |          0
27/01/2019 00:00:00 |      4 |      0 |          0
27/01/2019 00:00:00 |      4 |      1 |          1
28/01/2019 00:00:00 |      1 |      0 |          0
28/01/2019 00:00:00 |      1 |      1 |          0
28/01/2019 00:00:00 |      2 |      0 |          0
28/01/2019 00:00:00 |      2 |      1 |          0
28/01/2019 00:00:00 |      3 |      0 |          0
28/01/2019 00:00:00 |      3 |      1 |          0
28/01/2019 00:00:00 |      4 |      0 |          0
28/01/2019 00:00:00 |      4 |      1 |          0
29/01/2019 00:00:00 |      1 |      0 |          0
29/01/2019 00:00:00 |      1 |      1 |          1
29/01/2019 00:00:00 |      2 |      0 |          0
29/01/2019 00:00:00 |      2 |      1 |          1
29/01/2019 00:00:00 |      3 |      0 |          0
29/01/2019 00:00:00 |      3 |      1 |          0
29/01/2019 00:00:00 |      4 |      0 |          0
29/01/2019 00:00:00 |      4 |      1 |          0
30/01/2019 00:00:00 |      1 |      0 |          0
30/01/2019 00:00:00 |      1 |      1 |          0
30/01/2019 00:00:00 |      2 |      0 |          0
30/01/2019 00:00:00 |      2 |      1 |          0
30/01/2019 00:00:00 |      3 |      0 |          0
30/01/2019 00:00:00 |      3 |      1 |          1
30/01/2019 00:00:00 |      4 |      0 |          0
30/01/2019 00:00:00 |      4 |      1 |          0
31/01/2019 00:00:00 |      1 |      0 |          0
31/01/2019 00:00:00 |      1 |      1 |          0
31/01/2019 00:00:00 |      2 |      0 |          0
31/01/2019 00:00:00 |      2 |      1 |          0
31/01/2019 00:00:00 |      3 |      0 |          0
31/01/2019 00:00:00 |      3 |      1 |          0
31/01/2019 00:00:00 |      4 |      0 |          0
31/01/2019 00:00:00 |      4 |      1 |          0
01/02/2019 00:00:00 |      1 |      0 |          0
01/02/2019 00:00:00 |      1 |      1 |          0
01/02/2019 00:00:00 |      2 |      0 |          0
01/02/2019 00:00:00 |      2 |      1 |          0
01/02/2019 00:00:00 |      3 |      0 |          0
01/02/2019 00:00:00 |      3 |      1 |          0
01/02/2019 00:00:00 |      4 |      0 |          0
01/02/2019 00:00:00 |      4 |      1 |          0

db <>提琴here