我的查询遇到了一些麻烦。我试图得到当天的总进口率,同时也将前一天与相关时间相匹配。 示例:1AM(当天)= 20 凌晨1点(昨天)= 25。
截至目前,Current和Yesterday列显示相同的值。
SELECT
z.[import Hour],
COUNT(z.orderno) as CurrentDate,
COUNT(od.orderno) as Yesterday
FROM (
(select datepart(hh, recvtime) as [import Hour],
orderno
from mck_hvs.orderheader with (nolock)
where convert(date, recvtime) = convert(date, getdate())
and orderno not like '%ST'
UNION
select datepart(hh, recvtime) as [import Hour],
orderno from mck_hvs.oldorderheader with (nolock)
where convert(date, recvtime) = convert(date, getdate())
and orderno not like '%ST' ) as z
Join
(
select datepart(hh, od.recvtime) as [import Hour],
od.orderno from mck_hvs.oldorderheader od with (nolock)
where convert(date, od.recvtime) = convert(date, getdate()-1)
and od.orderno not like '%ST' ) as OD
ON z.[import Hour] = od.[import Hour] )
group by z.[import Hour]
答案 0 :(得分:0)
我预先包含了一些代码,只是为了生成一些测试数据。你可能不会需要它,但其他人可能会。
USE sandbox
GO
--------------------------------------------------------------------------------------------
--Recreate the OP's environment in my sandpit
DROP TABLE IF EXISTS mck_hvs.orderheader --2016 syntax
create table mck_hvs.orderheader(
recvtime datetime NOT NULL
,orderno varchar(20) NOT NULL
)
;
ALTER TABLE mck_hvs.orderheader
ADD CONSTRAINT PK_mck_hvs_orderheader_orderno PRIMARY KEY CLUSTERED (orderno)
;
GO
--------------------------------------------------------------------------------------------
DROP TABLE IF EXISTS mck_hvs.oldorderheader
;
create table mck_hvs.oldorderheader(
recvtime datetime NOT NULL
,orderno varchar(20) NOT NULL
)
;
ALTER TABLE mck_hvs.oldorderheader
ADD CONSTRAINT PK_mck_hvs_oldorderheader_orderno PRIMARY KEY CLUSTERED (orderno)
;
GO
--------------------------------------------------------------------------------------------
--Generate some test data (about two years worth working back from today)
--First the old data (not today's)
INSERT mck_hvs.oldorderheader
SELECT top 100000 dateadd(mi,abs(checksum(newid())) %1440,dateadd(dd,-abs(checksum(newid())) %720,getdate()-1)) as recvtime
,right('0000000' + cast(row_number() OVER(ORDER BY (SELECT NULL)) as varchar(20)),7)
+ char(82 + abs(checksum(newid()))%4) + char(82 + abs(checksum(newid()))%4) as orderno--add two random chars from R,S,T,U to the end
FROM sys.columns col1
cross join sys.columns col2
;
--Now today's (assume 500 orders came in)
INSERT mck_hvs.orderheader
SELECT top 500 dateadd(mi,abs(checksum(newid())) %1440,dateadd(dd,datediff(dd,0,getdate()),0)) --add a random number of minutes to midnight last night
,right('0000000' + cast(row_number() OVER(ORDER BY (SELECT NULL)) +100000 as varchar(20)),7)
+ char(82 + abs(checksum(newid()))%4) + char(82 + abs(checksum(newid()))%4) --add two random chars from R,S,T,U to the end
FROM sys.columns col1
cross join sys.columns col2
;
--------------------------------------------------------------------------------------------
WITH yesterday /*all my problems seemed so far away*/as
(
SELECT datepart(hour,old.recvtime) as received_hour
,count(*) as orders_received
FROM mck_hvs.oldorderheader as old
WHERE old.recvtime < dateadd(dd,datediff(dd,0,getdate()),0) --Yesterday
AND old.recvtime >= dateadd(dd,datediff(dd,0,getdate())-1,0)
AND old.orderno not like '%ST' --Note: This is not a SARGable search.
GROUP BY datepart(hour,recvtime)
)
, today as
(
SELECT datepart(hour,ordr.recvtime) as received_hour
,count(*) as orders_received
FROM mck_hvs.orderheader as ordr
WHERE ordr.orderno not like '%ST' --Google "SARG". Trust me.
GROUP BY datepart(hour,ordr.recvtime)
)
SELECT isnull(yesterday.received_hour,today.received_hour) as received_hour
,isnull(yesterday.orders_received,0) as orders_received_yesterday
,isnull(today.orders_received,0) as orders_received_today
FROM yesterday
--FULL JOIN in case there are hours of activity in one table that don't exist in the other table.
FULL JOIN today ON yesterday.received_hour = today.received_hour
;
答案 1 :(得分:0)
如果您使用以下数据:
DECLARE @Orders AS TABLE(OrderNo INT,OrderTaken datetime)
INSERT INTO @Orders VALUES(123,'2017-07-24 12:20:24')
INSERT INTO @Orders VALUES(124,'2017-07-24 12:30:24')
INSERT INTO @Orders VALUES(125,'2017-07-24 13:40:24')
INSERT INTO @Orders VALUES(126,'2017-07-24 13:50:24')
INSERT INTO @Orders VALUES(227,'2017-07-25 12:20:24')
INSERT INTO @Orders VALUES(228,'2017-07-25 12:30:24')
INSERT INTO @Orders VALUES(229,'2017-07-25 13:40:24')
INSERT INTO @Orders VALUES(220,'2017-07-25 13:50:24')
带输出:
以下SQL:
DECLARE @Date DATETIME='2017-07-25'
;WITH today AS(
SELECT Cast(OrderTaken As Date) oDate,
CASE WHEN DATEPART(hh,OrderTaken) > 12 THEN CONVERT(VARCHAR(2),DATEPART(hh,OrderTaken)-12) + ' PM' WHEN DATEPART(hh,OrderTaken) = 12 THEN CONVERT(VARCHAR(2),DATEPART(hh,OrderTaken)) + ' PM' WHEN DATEPART(hh,OrderTaken) < 12 THEN CONVERT(VARCHAR(2),DATEPART(hh,OrderTaken)) + ' AM' END As oHour,
Count(OrderNo) OrderCountToday FROM @Orders
WHERE CAST(OrderTaken AS DATE)=CAST(@Date AS DATE)
GROUP BY Cast(OrderTaken As Date),DATEPart(Hour,OrderTaken)
)
,yesterday AS(
SELECT Cast(OrderTaken As Date) oDate,
CASE WHEN DATEPART(hh,OrderTaken) > 12 THEN CONVERT(VARCHAR(2),DATEPART(hh,OrderTaken)-12) + ' PM' WHEN DATEPART(hh,OrderTaken) = 12 THEN CONVERT(VARCHAR(2),DATEPART(hh,OrderTaken)) + ' PM' WHEN DATEPART(hh,OrderTaken) < 12 THEN CONVERT(VARCHAR(2),DATEPART(hh,OrderTaken)) + ' AM' END As oHour,
Count(OrderNo) OrderCountYesterday
FROM @Orders
WHERE CAST(OrderTaken AS DATE)=CAST(@Date-1 AS DATE)
GROUP BY Cast(OrderTaken As Date),DATEPart(Hour,OrderTaken)
)
SELECT t.oDate Today,t.oHour Hour,t.OrderCountToday,y.OrderCountYesterday FROM today t join yesterday y ON t.oHour=y.oHour and t.oDate=dateadd(day,1,y.oDate)
将导致:
p.s。:如果你不想在一小时内使用AM / PM,你可以总结一下这个SQL。甚至可以进一步总结AM / PM的逻辑,但会将12 PM
显示为0 PM
。
希望得到这个帮助。