我正在尝试加入这两个表,或使用子查询来实现我需要的结果。 截至目前,我有第一个查询拉小时总计,总时间在MIN(时间)和MAX(时间)之间。我需要加入第二个查询以从MIN(时间)和MAX(时间)获得总扫描 我可能会出错的任何想法?
select
UPPER(t.operator) as Operator,
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 6 THEN 1 ELSE 0 END)) ELSE NULL END AS '6AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 7 THEN 1 ELSE 0 END)) ELSE NULL END AS '7AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 8 THEN 1 ELSE 0 END)) ELSE NULL END AS '8AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 9 THEN 1 ELSE 0 END)) ELSE NULL END AS '9AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 10 THEN 1 ELSE 0 END)) ELSE NULL END AS '10AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 11 THEN 1 ELSE 0 END)) ELSE NULL END AS '11AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 12 THEN 1 ELSE 0 END)) ELSE NULL END AS '12PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 13 THEN 1 ELSE 0 END)) ELSE NULL END AS '1PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 14 THEN 1 ELSE 0 END)) ELSE NULL END AS '2PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 15 THEN 1 ELSE 0 END)) ELSE NULL END AS '3PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 16 THEN 1 ELSE 0 END)) ELSE NULL END AS '4PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 17 THEN 1 ELSE 0 END)) ELSE NULL END AS '5PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (COUNT(CASE WHEN DATEPART(HOUR,t.time) > 17 THEN t.eventtype ELSE NULL END)) ELSE NULL END AS '6+PM',
SUM(CASE WHEN t.eventtype = 'Replenishment Complete' THEN 1 ELSE 0 END) as TotalCanisters
from mck_hvs.replevent t
where
CAST(t.time as DATE) = CAST(GETDATE() as DATE)
and t.stationtype IN ('T', 'S')
and t.eventtype = 'Replenishment Complete'
group by
t.operator,
t.eventtype
第二次查询:
select
UPPER(o.operator) as Operator,
SUM(CASE WHEN o.eventtype = 'GoodScan' THEN 1 ELSE 0 END) as TotalScans,
convert( varchar(19), MIN(o.time), 8) as LogonTime,
DATEDIFF(MINUTE, MIN(o.time), MAX(o.time)) as TotalMinLogon
from mck_hvs.replevent o
where
CAST(o.time as DATE) = CAST(GETDATE() as DATE)
and o.eventtype IN ( 'Replenishment Complete', 'GoodScan' )
group by
o.operator
order by o.operator
答案 0 :(得分:1)
下面的内容应该让你找到解决方案。我删除了t.eventtype上的组并从case语句中删除了,因为where子句已经为你限制了结果。
select
op.Operator,
op.TotalScans,
op.LogonTime,
op.TotalMinLogon,
b.[6AM],
b.[7AM],
b.[8AM],
b.[9AM],
b.[10AM],
b.[11AM],
b.[12PM],
b.[1PM],
b.[2PM],
b.[3PM],
b.[4PM],
b.[5PM],
b.[6+PM]
from
(
select
UPPER(o.operator) as Operator,
SUM(CASE WHEN o.eventtype = 'GoodScan' THEN 1 ELSE 0 END) as TotalScans,
convert( varchar(19), MIN(o.time), 8) as LogonTime,
DATEDIFF(MINUTE, MIN(o.time), MAX(o.time)) as TotalMinLogon
from mck_hvs.replevent o
where
--will use o.time index now:
o.time >= CAST(GETDATE() as DATE)
and o.time < CAST(dateadd(Day,1,GETDATE()) as DATE)
and o.eventtype IN ( 'Replenishment Complete', 'GoodScan' )
group by
o.operator
) as op
left join
(
select
UPPER(t.operator) as Operator,
SUM(CASE WHEN DATEPART(HOUR,t.time) = 6 THEN 1 ELSE 0 END) AS '6AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 7 THEN 1 ELSE 0 END) AS '7AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 8 THEN 1 ELSE 0 END) AS '8AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 9 THEN 1 ELSE 0 END) AS '9AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 10 THEN 1 ELSE 0 END) AS '10AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 11 THEN 1 ELSE 0 END) AS '11AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 12 THEN 1 ELSE 0 END) AS '12PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 13 THEN 1 ELSE 0 END) AS '1PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 14 THEN 1 ELSE 0 END) AS '2PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 15 THEN 1 ELSE 0 END) AS '3PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 16 THEN 1 ELSE 0 END) AS '4PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 17 THEN 1 ELSE 0 END) AS '5PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) > 17 THEN 1 ELSE 0 END) AS '6+PM',
SUM(CASE WHEN t.eventtype = 'Replenishment Complete' THEN 1 ELSE 0 END) as TotalCanisters
from mck_hvs.replevent t
where
--will use o.time index now:
o.time >= CAST(GETDATE() as DATE)
and o.time < CAST(dateadd(Day,1,GETDATE()) as DATE)
and t.stationtype IN ('T', 'S')
and t.eventtype = 'Replenishment Complete'
group by
t.operator
) as b
on b.Operator = op.Operator
答案 1 :(得分:0)
SELECT l.* ,
r.TotalScans ,
r.LogonTime ,
r.TotalMinLogon
FROM ( SELECT UPPER(t.operator) AS Operator ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 6
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '6AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 7
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '7AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 8
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '8AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 9
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '9AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 10
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '10AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 11
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '11AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 12
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '12PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 13
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '1PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 14
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '2PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 15
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '3PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 16
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '4PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 17
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '5PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( COUNT(CASE WHEN DATEPART(HOUR, t.time) > 17
THEN t.eventtype
ELSE NULL
END) )
ELSE NULL
END AS '6+PM' ,
SUM(CASE WHEN t.eventtype = 'Replenishment Complete'
THEN 1
ELSE 0
END) AS TotalCanisters
FROM @replevent t
WHERE CAST(t.time AS DATE) = CAST(GETDATE() AS DATE)
AND t.stationtype IN ( 'T', 'S' )
AND t.eventtype = 'Replenishment Complete'
GROUP BY t.operator ,
t.eventtype
) AS l
INNER JOIN ( SELECT UPPER(o.operator) AS Operator_r ,
SUM(CASE WHEN o.eventtype = 'GoodScan' THEN 1
ELSE 0
END) AS TotalScans ,
CONVERT(VARCHAR(19), MIN(o.time), 8) AS LogonTime ,
DATEDIFF(SECOND, MIN(o.time), MAX(o.time)) AS TotalMinLogon
FROM @replevent o
WHERE CAST(o.time AS DATE) = CAST(GETDATE() AS DATE)
AND o.eventtype IN ( 'Replenishment Complete',
'GoodScan' )
GROUP BY o.operator
) AS r ON l.Operator = r.Operator_r
ORDER BY l.Operator;