选择日期范围,根据关闭条件计算范围内的多条记录,按班次分组

Posted

技术标签:

【中文标题】选择日期范围,根据关闭条件计算范围内的多条记录,按班次分组【英文标题】:Pick Date Range, Count multiple Records in range based off condition, group by shift 【发布时间】:2014-02-06 17:15:07 【问题描述】:

所以我是 SQL World 的新手,所以请原谅这个问题可能是多么无知。我最终可能需要为此执行一个存储过程,但在进入下一步之前尝试让它在查询中工作。

我正在尝试执行以下操作:

    选择特定日期之间的记录

    计算 where 语句中给定条件的记录。我有 6 条不同的记录要计数,每次计数都需要一个新列。

    然后我想按 A 或 B 分组

我可以像下面这样计算记录。

     SELECT        (SELECT        COUNT(*) AS 6F
                      FROM            dbo.1_Data
                      WHERE        ([B] BETWEEN 1 AND 9) AND (Time BETWEEN CONVERT(DATETIME, '2014-01-01 00:00:00', 102) AND CONVERT(DATETIME, 
                                                '2014-02-01 00:00:00', 102))) AS 6F,
                         (SELECT        COUNT(*) AS 5E
                           FROM            dbo.1_Data AS 1_Data_1
                           WHERE        ([B] = 10) AND (Time BETWEEN CONVERT(DATETIME, '2014-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2014-02-01 00:00:00',
                                                      102))) AS 5E,
                         (SELECT        COUNT(*) AS 4D
                           FROM            dbo.1_Data AS 1_Data_2
                           WHERE        ([B] BETWEEN 11 AND 13) AND (Time BETWEEN CONVERT(DATETIME, '2014-01-01 00:00:00', 102) AND CONVERT(DATETIME, 
                                                     '2014-02-01 00:00:00', 102))) AS 4D,
                         (SELECT        COUNT(*) AS 1A
                           FROM            dbo.1_Data AS 1_Data_3
                           WHERE        ([B] = 14) AND (Time BETWEEN CONVERT(DATETIME, '2014-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2014-02-01 00:00:00',
                                                      102))) AS 1A,
                         (SELECT        COUNT(*) AS 2B
                           FROM            dbo.1_Data AS 1_Data_4
                           WHERE        ([A Step] BETWEEN 1 AND 25) AND (Time BETWEEN CONVERT(DATETIME, '2014-01-01 00:00:00', 102) AND CONVERT(DATETIME, 
                                                     '2014-02-01 00:00:00', 102))) AS 2B,
                         (SELECT        COUNT(*) AS 3C
                           FROM            dbo.1_Data AS 1_Data_5
                           WHERE        ([B Step] = 0) AND ([ A Step] = 0) AND (Time BETWEEN CONVERT(DATETIME, '2014-01-01 00:00:00', 102) AND 
                                                     CONVERT(DATETIME, '2014-02-01 00:00:00', 102))) AS 3C

我可以这样做以获取时间范围内给定步骤中的记录数并进行分组。

    SELECT COUNT([B]) AS [1A], Letter

    FROM  dbo.1_Data

    WHERE (Time BETWEEN CONVERT(DATETIME, '2014-01-05 00:00:00', 102) AND CONVERT(DATETIME, '2014-01-10 00:00:00', 102)) AND ([B] = 14)

    GROUP BY Letter

我正在寻找与此类似的最终结果。列名在顶部,记录数在其下方。

1A 的列,A 的记录计数和 B 的计数。

|1A |信 |


|34 |一个 |


|93 |乙|

如何将所有这些合并到一个查询语句中,以便在给定的日期范围和条件内整理出数据,然后按字母分组?

【问题讨论】:

【参考方案1】:

我仍然对你想要什么感到困惑,但我认为你需要这个

SELECT COUNT(CASE WHEN ( [B] BETWEEN 1 AND 9 )
                       AND ( [Time] BETWEEN '2014-01-01 00:00:00' AND '2014-02-01 00:00:00' ) THEN 1
                  ELSE NULL
             END) AS '6f'
       ,COUNT(CASE WHEN ( [B] = 10 )
                        AND ( [Time] BETWEEN '2014-01-01 00:00:00' AND '2014-02-01 00:00:00' ) THEN 1
                   ELSE NULL
              END) AS '5E'
       ,COUNT(CASE WHEN ( [B] BETWEEN 11 AND 13 )
                        AND ( [Time] BETWEEN '2014-01-01 00:00:00' AND '2014-02-01 00:00:00' ) THEN 1
                   ELSE NULL
              END) AS '4D'
       ,COUNT(CASE WHEN ( [B] = 14 )
                        AND ( [Time] BETWEEN '2014-01-01 00:00:00' AND '2014-02-01 00:00:00' ) THEN 1
                   ELSE NULL
              END) AS '1A'
       ,COUNT(CASE WHEN ( [A Step] BETWEEN 1 AND 25 )
                        AND ( [Time] BETWEEN '2014-01-01 00:00:00' AND '2014-02-01 00:00:00' ) THEN 1
                   ELSE NULL
              END) AS '2B'
       ,COUNT(CASE WHEN [B Step] = 0
                        AND [ A Step] = 0
                        AND ( [Time] BETWEEN '2014-01-01 00:00:00' AND '2014-02-01 00:00:00' ) THEN 1
                   ELSE NULL
              END) AS '3C'
    FROM dbo.[1_Data]
您不需要将 '2014-01-01' 转换为 DATETIME SQL Server 会自动执行此操作 您可以在CASE 语句和CASE 语句的COUNT 结果中放置逻辑,计数时请记住NULL 您的else 结果,否则它将计算所有内容。

【讨论】:

那行得通。我希望有一种方法只需要更改一次 StartDate 和 EndDate 。但是我可以使用存储过程中的参数来解决这个问题。感谢您的帮助。我已将您的回复标记为已回答。 没关系。我的错。这将与那里的正确 where 语句完美配合。再次感谢您帮助解决这个问题。

以上是关于选择日期范围,根据关闭条件计算范围内的多条记录,按班次分组的主要内容,如果未能解决你的问题,请参考以下文章

选择日期范围内的特定日期

我们可以使用group by和where字段名相同的条件

SQL 计算项目状态历史记录在日期范围内的项目项

根据包含日期范围的观察数量按日期计算

SQL 如何查询日期在一定范围内的数据

按日期范围删除数据框中的行。