使用SQL SERVER查询为每小时(小时)选择前1

时间:2016-12-07 06:51:03

标签: sql sql-server

我需要一个查询来在两个日期时间之间按小时获取一些记录列表,我想从每小时获取最后一条记录,如果数据表中不存在数据,那么我需要显示小时明智但{ {1}}默认值应为零。

结果应该像下面那样

importwh

例如数据

SNO Startdate               enddate                 ImportWH  
1   2016-12-05 00:00:00.000 2016-12-05 00:59:59.000   0  
2   2016-12-05 01:00:00.000 2016-12-05 01:59:59.000   0  
3   2016-12-05 02:00:00.000 2016-12-05 02:59:59.000   0  
4   2016-12-05 03:00:00.000 2016-12-05 03:59:59.000   0  
5   2016-12-05 04:00:00.000 2016-12-05 04:59:59.000   0  
6   2016-12-05 05:00:00.000 2016-12-05 05:59:59.000   0  
7   2016-12-05 06:00:00.000 2016-12-05 06:59:59.000   0  
8   2016-12-05 07:00:00.000 2016-12-05 07:59:59.000   0  
9   2016-12-05 08:00:00.000 2016-12-05 08:59:59.000   0  
10  2016-12-05 09:00:00.000 2016-12-05 09:59:59.000   0  
11  2016-12-05 10:00:00.000 2016-12-05 10:59:59.000   0  
12  2016-12-05 11:00:00.000 2016-12-05 11:59:59.000   0  
13  2016-12-05 12:00:00.000 2016-12-05 12:59:59.000   0  
14  2016-12-05 13:00:00.000 2016-12-05 13:59:59.000   0  
15  2016-12-05 14:00:00.000 2016-12-05 14:59:59.000   0  
16  2016-12-05 15:00:00.000 2016-12-05 15:59:59.000   0  
17  2016-12-05 16:00:00.000 2016-12-05 16:59:59.000   0  
18  2016-12-05 17:00:00.000 2016-12-05 17:59:59.000   0  
19  2016-12-05 18:00:00.000 2016-12-05 18:59:59.000   0  
20  2016-12-05 19:00:00.000 2016-12-05 19:59:59.000   0  
21  2016-12-05 20:00:00.000 2016-12-05 20:59:59.000   0  
22  2016-12-05 21:00:00.000 2016-12-05 21:59:59.000   0  
23  2016-12-05 22:00:00.000 2016-12-05 22:59:59.000   0  
24  2016-12-05 23:00:00.000 2016-12-07 23:59:59.000   0  

应该像下面这样的结果       SNO Startdate enddate ImportWH

SNO Startdate               enddate                 ImportWH  
1   2016-12-05 00:00:00.000 2016-12-05 00:59:59.000   0.1  
2   2016-12-05 00:00:00.000 2016-12-05 00:59:59.000   0.1  
3   2016-12-05 00:00:00.000 2016-12-05 00:59:59.000   2  
4   2016-12-05 01:00:00.000 2016-12-05 01:59:59.000   1  
5   2016-12-05 01:00:00.000 2016-12-05 01:59:59.000   2   

3 个答案:

答案 0 :(得分:0)

您想生成此列表吗?

    DECLARE   @StartDate DATETIME=CONVERT(VARCHAR,GETDATE(),110)
    SELECT sv.number AS SNO,DATEADD(hh,sv.number-1,@StartDate) AS Startdate,DATEADD(SECOND,-1,DATEADD(hh,sv.number,@StartDate)) AS enddate,0 AS ImportWH
    FROM master.dbo.spt_values AS sv
    WHERE sv.type='P' AND sv.number BETWEEN 1 AND 24
SNO         Startdate               enddate                 ImportWH
----------- ----------------------- ----------------------- -----------
1           2016-12-07 00:00:00.000 2016-12-07 00:59:59.000 0
2           2016-12-07 01:00:00.000 2016-12-07 01:59:59.000 0
3           2016-12-07 02:00:00.000 2016-12-07 02:59:59.000 0
4           2016-12-07 03:00:00.000 2016-12-07 03:59:59.000 0
5           2016-12-07 04:00:00.000 2016-12-07 04:59:59.000 0
6           2016-12-07 05:00:00.000 2016-12-07 05:59:59.000 0
7           2016-12-07 06:00:00.000 2016-12-07 06:59:59.000 0
8           2016-12-07 07:00:00.000 2016-12-07 07:59:59.000 0
9           2016-12-07 08:00:00.000 2016-12-07 08:59:59.000 0
10          2016-12-07 09:00:00.000 2016-12-07 09:59:59.000 0
11          2016-12-07 10:00:00.000 2016-12-07 10:59:59.000 0
12          2016-12-07 11:00:00.000 2016-12-07 11:59:59.000 0
13          2016-12-07 12:00:00.000 2016-12-07 12:59:59.000 0
14          2016-12-07 13:00:00.000 2016-12-07 13:59:59.000 0
15          2016-12-07 14:00:00.000 2016-12-07 14:59:59.000 0
16          2016-12-07 15:00:00.000 2016-12-07 15:59:59.000 0
17          2016-12-07 16:00:00.000 2016-12-07 16:59:59.000 0
18          2016-12-07 17:00:00.000 2016-12-07 17:59:59.000 0
19          2016-12-07 18:00:00.000 2016-12-07 18:59:59.000 0
20          2016-12-07 19:00:00.000 2016-12-07 19:59:59.000 0
21          2016-12-07 20:00:00.000 2016-12-07 20:59:59.000 0
22          2016-12-07 21:00:00.000 2016-12-07 21:59:59.000 0
23          2016-12-07 22:00:00.000 2016-12-07 22:59:59.000 0
24          2016-12-07 23:00:00.000 2016-12-07 23:59:59.000 0

Mehtod 1,可以获得不在组中的其他列,例如SNO:

WITH TB(SNO,Startdate,enddate ,ImportWH)AS(

   SELECT 1,'2016-12-05 00:00:00.000','2016-12-05 00:59:59.000', 0.1 UNION
   SELECT 2,'2016-12-05 00:00:00.000','2016-12-05 00:59:59.000',0.1  UNION
   SELECT 3,'2016-12-05 00:00:00.000','2016-12-05 00:59:59.000',2  UNION
   SELECT 4,'2016-12-05 01:00:00.000','2016-12-05 01:59:59.000',1  UNION
   SELECT 5,'2016-12-05 01:00:00.000','2016-12-05 01:59:59.000',2  
)
SELECT * FROM (
   SELECT *,ROW_NUMBER()OVER(PARTITION BY Startdate,enddate ORDER BY ImportWH desc) AS rn FROM tb
) t WHERE t.rn=1
SNO         Startdate               enddate                 ImportWH                                rn
----------- ----------------------- ----------------------- --------------------------------------- --------------------
3           2016-12-05 00:00:00.000 2016-12-05 00:59:59.000 2.0                                     1
5           2016-12-05 01:00:00.000 2016-12-05 01:59:59.000 2.0                                     1

方法2,分组BY

WITH TB(SNO,Startdate,enddate ,ImportWH)AS(

   SELECT 1,'2016-12-05 00:00:00.000','2016-12-05 00:59:59.000', 0.1 UNION
   SELECT 2,'2016-12-05 00:00:00.000','2016-12-05 00:59:59.000',0.1  UNION
   SELECT 3,'2016-12-05 00:00:00.000','2016-12-05 00:59:59.000',2  UNION
   SELECT 4,'2016-12-05 01:00:00.000','2016-12-05 01:59:59.000',1  UNION
   SELECT 5,'2016-12-05 01:00:00.000','2016-12-05 01:59:59.000',2  
)

SELECT Startdate,enddate,MAX(ImportWH) AS ImportWH FROM tb GROUP BY Startdate,enddate
Startdate               enddate                 ImportWH
----------------------- ----------------------- ---------------------------------------
2016-12-05 00:00:00.000 2016-12-05 00:59:59.000 2.0
2016-12-05 01:00:00.000 2016-12-05 01:59:59.000 2.0

答案 1 :(得分:0)

declare @test table (sno int,startdate datetime,enddate datetime,importwh numeric(5,2))

insert into @test values (1,'2016-12-05 00:00:00.000','2016-12-05 00:59:59.000','0.1');  
insert into @test values (2,'2016-12-05 00:00:00.000','2016-12-05 00:59:59.000','0.1');  
insert into @test values (3,'2016-12-05 00:00:00.000','2016-12-05 00:59:59.000','2');
insert into @test values (4,'2016-12-05 01:00:00.000','2016-12-05 01:59:59.000','1');
insert into @test values (5,'2016-12-05 01:00:00.000','2016-12-05 01:59:59.000','2');

select '1',startdate,enddate,max(importwh) from @test group by startdate,enddate

答案 2 :(得分:0)

据我了解,您希望每小时统计数据。

1,首先生成时间序列基本ID和日(每个ID和日将生成相关时间序列。 2.按小时加入您的日期和分组,并使用最大ImportWH值 3.我修改了你的数据,添加了一个新的ID。

 WITH table1(SNO,ID,IDAliasname,createddate,ImportWH)AS(

        SELECT 48883,'867330022903407','SG2003',CONVERT(DATETIME,'2016-12-07 15:15:00.000'),45.266 UNION ALL
        SELECT 48884,'867330022903407','SG2003','2016-12-07 15:46:00.000',45.266 UNION ALL
        SELECT 48885,'867330022903407','SG2003','2016-12-07 16:02:00.000',45.266 UNION ALL
        SELECT 48886,'867330022903407','SG2003','2016-12-07 16:17:00.000',45.266 UNION ALL
        SELECT 48887,'867330022903408','SG2004','2016-12-07 15:15:00.000',45.266 UNION ALL
        SELECT 48888,'867330022903408','SG2004','2016-12-07 15:31:00.000',45.266 UNION ALL
        SELECT 48889,'867330022903408','SG2004','2016-12-07 15:46:00.000',45.266 UNION ALL
        SELECT 48890,'867330022903408','SG2004','2016-12-07 16:02:00.000',45.266 UNION ALL
        SELECT 48891,'867330022903408','SG2004','2016-12-07 16:17:00.000',45.266 UNION ALL
        SELECT 48892,'867330022903408','SG2004','2016-12-07 16:33:00.000',45.266
    )
    --SELECT * FROM table1
    SELECT  t.ID,t.IDAliasname,t.StartTime,t.EndTime,MAX(ImportWH) AS ImportWH FROM (
        SELECT d.dt,i.IDAliasname,i.ID,DATEADD(HOUR, sv.number-1,d.dt) AS StartTime,DATEADD(SECOND,-1, DATEADD(HOUR, sv.number,d.dt)) AS  EndTime
        FROM master.dbo.spt_values AS sv
        ,(SELECT DISTINCT CONVERT(VARCHAR,table1.createddate,110) AS dt FROM table1)  AS d
        ,(SELECT DISTINCT ID, IDAliasname FROM table1)  AS i
        WHERE sv.type='P' AND sv.number BETWEEN 1 AND 24
      ) AS t
    INNER JOIN table1 AS tt ON tt.IDAliasname=t.IDAliasname AND t.ID=tt.ID AND DATEDIFF(SECOND,t.StartTime,tt.createddate)>=0 AND DATEDIFF(SECOND,tt.createddate,t.EndTime)>=0
    GROUP BY t.ID,t.IDAliasname,t.StartTime,t.EndTime
ID              IDAliasname StartTime               EndTime                 ImportWH
--------------- ----------- ----------------------- ----------------------- ---------------------------------------
867330022903407 SG2003      2016-12-07 15:00:00.000 2016-12-07 15:59:59.000 45.266
867330022903407 SG2003      2016-12-07 16:00:00.000 2016-12-07 16:59:59.000 45.266
867330022903408 SG2004      2016-12-07 15:00:00.000 2016-12-07 15:59:59.000 45.266
867330022903408 SG2004      2016-12-07 16:00:00.000 2016-12-07 16:59:59.000 45.266