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

时间:2014-02-06 17:15:07

标签: sql count subquery

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

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

  1. 选择某些日期之间的记录

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

  3. 然后我想按A或B分组

  4. 我可以计算下面的记录。

         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 | A |


    | 93 | B |

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

1 个答案:

答案 0 :(得分:0)

我仍然对你想要的东西感到困惑,但我认为你需要这个

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语句和COUNT语句的CASE结果中,在计算时请记住NULL您的其他结果,否则它将计算所有内容。