如何在SQL Server中以小时为单位从表中获取记录?

时间:2015-06-04 12:42:23

标签: sql-server

我有一个名为DailySales的表(ID int,Date date,Time Time,CashSale Money,CardSale money,TotalSale money)。现在我想低于结果......

                  Date : 04-06-2015

                  Time : 09:01 - 10:00
CashSale           CardSale                   TotalSale
10000.00           15000.00                   25000.00

                 Time : 10:01 - 11:00
CashSale           CardSale                   TotalSale
20000.00           15000.00                   35000.00
.
.
.

请帮帮我, 感谢..

2 个答案:

答案 0 :(得分:0)

我建议您最好将日期/时间存储在datetimedatetime2列中,而不是仅存储时间,然后使用可以使用{{1获取时间的函数。

假设datepart列实际上是Time,您可以执行以下操作:

DateTime

这会在一小时一小时(例如09:00:00至09:59:59,10:00至10:59:59等)将其分开。如果您想要偏移分割的位置,例如在一小时后的1分钟,则可以使用SELECT Date, datepart(Hour, Time) AS Hour, sum(CashSale) AS ByCash, sum(CardSale) AS ByCard, sum(TotalSale) AS NetSale FROM DailySales GROUP BY Date, datepart(Hour, Time) 函数,例如dateadd代替dateadd(minute, -1, Time)。< / p>

如果您以某种其他方式存储字符串,例如作为字符串,那么您希望根据该字符串表达式对字符串进行分组,例如Time

答案 1 :(得分:0)

这只是一个如何实现这一目标的提示。这只是sql可以为您做的最大值。其余的应该在报告中完成:

准备数据:

DECLARE @t TABLE
    (
      Date DATE ,
      Time TIME ,
      CashSale MONEY ,
      CardSale MONEY ,
      TotalSale MONEY
    )

INSERT  INTO @t
VALUES  ( '20150603', '09:30', 10, 5, 15 ),
        ( '20150604', '09:02', 10, 5, 15 ),
        ( '20150604', '09:30', 10, 5, 15 ),
        ( '20150604', '10:00', 10, 5, 15 ),
        ( '20150604', '11:01', 10, 5, 15 ),
        ( '20150604', '11:30', 10, 5, 15 ),
        ( '20150604', '12:30', 10, 5, 15 );

查询:

WITH times
AS ( SELECT   *
     FROM ( VALUES
          ( CAST('09:01' AS TIME), CAST('10:00' AS TIME)),
          ( CAST('10:01' AS TIME), CAST('11:00' AS TIME)),
          ( CAST('11:01' AS TIME), CAST('12:00' AS TIME)) ) times ( StartTime, EndTime )
             )
    SELECT  *
    FROM    ( SELECT    t.Date ,
                        ti.StartTime ,
                        ti.EndTime ,
                        SUM(CashSale) CashSale ,
                        SUM(CardSale) CardSale ,
                        SUM(TotalSale) TotalSale
              FROM      @t t
                        JOIN times ti ON t.Time BETWEEN ti.StartTime AND ti.EndTime
              WHERE     Date = '20150604'
              GROUP BY  t.Date ,
                        ti.StartTime ,
                        ti.EndTime
            ) t UNPIVOT( Amount FOR SaleType IN ( [CashSale], [CardSale], [TotalSale] ) ) u
    ORDER BY Date ,
            StartTime ,
            EndTime ,
            CASE SaleType
              WHEN 'TotalSale' THEN 1
              WHEN 'CashSale' THEN 2
              WHEN 'CardSale' THEN 3
            END

输出:

Date        StartTime           EndTime             Amount  SaleType
2015-06-04  09:01:00.0000000    10:00:00.0000000    45.00   TotalSale
2015-06-04  09:01:00.0000000    10:00:00.0000000    30.00   CashSale
2015-06-04  09:01:00.0000000    10:00:00.0000000    15.00   CardSale
2015-06-04  11:01:00.0000000    12:00:00.0000000    30.00   TotalSale
2015-06-04  11:01:00.0000000    12:00:00.0000000    20.00   CashSale
2015-06-04  11:01:00.0000000    12:00:00.0000000    10.00   CardSale

修改

您可以创建一个表格:

Times(StartTime TIME, EndTime TIME)

在其中插入所有24行:

INSERT INTO Times VALUES
( CAST('09:01' AS TIME), CAST('10:00' AS TIME)),
( CAST('10:01' AS TIME), CAST('11:00' AS TIME)),
( CAST('11:01' AS TIME), CAST('12:00' AS TIME)),
....
( CAST('23:01' AS TIME), CAST('00:00' AS TIME))

你可以使用它:

SELECT  *
FROM    ( SELECT    t.Date ,
                    ti.StartTime ,
                    ti.EndTime ,
                    SUM(CashSale) CashSale ,
                    SUM(CardSale) CardSale ,
                    SUM(TotalSale) TotalSale
          FROM      @t t
                    JOIN Times ti ON t.Time BETWEEN ti.StartTime AND ti.EndTime
          WHERE     Date = '20150604'
          GROUP BY  t.Date ,
                    ti.StartTime ,
                    ti.EndTime
        ) t UNPIVOT( Amount FOR SaleType IN ( [CashSale], [CardSale], [TotalSale] ) ) u
ORDER BY Date ,
        StartTime ,
        EndTime ,
        CASE SaleType
          WHEN 'TotalSale' THEN 1
          WHEN 'CashSale' THEN 2
          WHEN 'CardSale' THEN 3
        END