我没有得到使用SQL Server从Fromdate到Todate获取数据的小时列表。
我的表数据:table1
SNO ID IDAliasname createddate ImportWH
48887 867330022903408 SG2004 2016-12-07 15:15:00.000 45.266
48888 867330022903408 SG2004 2016-12-07 15:31:00.000 45.266
48889 867330022903408 SG2004 2016-12-07 15:46:00.000 45.266
48890 867330022903408 SG2004 2016-12-07 16:02:00.000 45.266
48891 867330022903408 SG2004 2016-12-07 16:17:00.000 45.266
48892 867330022903408 SG2004 2016-12-07 16:33:00.000 45.266
我的查询
IF OBJECT_ID('tempdb..#YourTempTable') IS NOT NULL
DROP TABLE #YourTempTable
GO
SELECT *
INTO #YourTempTable
FROM (
SELECT
dateadd(hour, datediff(hour, 0, createddate), 0) as Startdate,
dateadd(second, -1, dateadd(hour, 1+datediff(hour, 0, createddate), 0)) as enddate ,IDAliasname
,ID,ImportWH
FROM table1
WHERE IDAliasname='SG2004'
AND createddate BETWEEN '2016-12-07 00:00:00.000'
AND '2016-12-07 23:59:00') a
select *
from #YourTempTable
select IDAliasname,ID,Startdate,enddate,max(ImportWH)
from #YourTempTable
group by startdate,enddate,IDAliasname,ID
我的结果低于结果
SG2004 867330022903408 2016-12-07 15:00:00.000 2016-12-07 15:59:59.000 45.266
SG2004 867330022903408 2016-12-07 16:00:00.000 2016-12-07 16:59:59.000 45.266
我需要从startdate 2016-12-07 00:00:00.000
到2016-12-07 23:59:00.000
的数据,我需要如下结果...请查找数据ike bellow
IDAliasname ID Startdate enddate ImportWH
SG2004 867330022903408 2016-12-07 00:00:00.000 2016-12-07 00:59:59.000 0
SG2004 867330022903408 2016-12-07 01:00:00.000 2016-12-07 01:59:59.000 0
SG2004 867330022903408 2016-12-07 02:00:00.000 2016-12-07 02:59:59.000 0
SG2004 867330022903408 2016-12-07 03:00:00.000 2016-12-07 03:59:59.000 0
SG2004 867330022903408 2016-12-07 04:00:00.000 2016-12-07 04:59:59.000 0
SG2004 867330022903408 2016-12-07 05:00:00.000 2016-12-07 05:59:59.000 0
SG2004 867330022903408 2016-12-07 06:00:00.000 2016-12-07 06:59:59.000 0
SG2004 867330022903408 2016-12-07 07:00:00.000 2016-12-07 07:59:59.000 0
SG2004 867330022903408 2016-12-07 08:00:00.000 2016-12-07 08:59:59.000 0
SG2004 867330022903408 2016-12-07 09:00:00.000 2016-12-07 09:59:59.000 0
SG2004 867330022903408 2016-12-07 10:00:00.000 2016-12-07 10:59:59.000 0
SG2004 867330022903408 2016-12-07 11:00:00.000 2016-12-07 11:59:59.000 0
SG2004 867330022903408 2016-12-07 12:00:00.000 2016-12-07 12:59:59.000 0
SG2004 867330022903408 2016-12-07 13:00:00.000 2016-12-07 13:59:59.000 0
SG2004 867330022903408 2016-12-07 14:00:00.000 2016-12-07 14:59:59.000 0
SG2004 867330022903408 2016-12-07 15:00:00.000 2016-12-07 15:59:59.000 45.266
SG2004 867330022903408 2016-12-07 16:00:00.000 2016-12-07 16:59:59.000 45.266
SG2004 867330022903408 2016-12-07 17:00:00.000 2016-12-07 17:59:59.000 0
SG2004 867330022903408 2016-12-07 18:00:00.000 2016-12-07 18:59:59.000 0
SG2004 867330022903408 2016-12-07 19:00:00.000 2016-12-07 19:59:59.000 0
SG2004 867330022903408 2016-12-07 20:00:00.000 2016-12-07 20:59:59.000 0
SG2004 867330022903408 2016-12-07 21:00:00.000 2016-12-07 21:59:59.000 0
SG2004 867330022903408 2016-12-07 22:00:00.000 2016-12-07 22:59:59.000 0
SG2004 867330022903408 2016-12-07 23:00:00.000 2016-12-07 23:59:59.000 0
答案 0 :(得分:0)
我认为您需要这样的查询:
-- By using a CTE creating 24 rows for each hour
WITH hoursTable(i, startHour, endHour) as (
SELECT 1, CAST(CAST('0:00:00' as time) as datetime), cast(cast('0:59:59' as time) as datetime)
UNION ALL
SELECT i+1, DATEADD(HOUR,1,startHour), DATEADD(HOUR,1,endHour)
FROM hoursTable
WHERE i < 24
),
-- Then you need to grouping your data based on each days
dateTable as (
SELECT IDAliasname, ID, CAST(CAST(createddate AS date) as datetime) justDate
FROM table1
GROUP BY IDAliasname, ID, CAST(CAST(createddate AS date) as datetime)
)
SELECT dateTable.IDAliasname, dateTable.ID, justDate + startHour as Startdate, justDate + endHour as enddate, COALESCE(AVG(table1.ImportWH), 0) ImportWH
FROM dateTable CROSS JOIN hoursTable -- Here you have combination
LEFT JOIN table1 ON dateTable.IDAliasname = table1.IDAliasname AND dateTable.ID = table1.ID AND table1.createddate BETWEEN justDate + startHour AND justDate + endHour
WHERE justDate + startHour >= '2016-12-07 00:00:00'
AND justDate + endHour <= '2016-12-07 23:59:59'
GROUP BY dateTable.IDAliasname, dateTable.ID, justDate + startHour, justDate + endHour;
答案 1 :(得分:0)
你想要这个
吗? WITH table1(SNO,ID,IDAliasname,createddate,ImportWH)AS(
SELECT 48887,'867330022903408','SG2004',CONVERT(DATETIME,'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 --------------- ----------- ----------------------- ----------------------- --------------------------------------- 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