如何检索每个班次第一个小时的时间戳计数?
How do I retrieve a count of timestamps over the first hour of each shift?
好吧,这个可能很难解释..
我想做的是计算每个班次的第一个小时内的负载数量。我的意思是,如果加载从 0630 开始,我想计算它从 0630 到 0700 完成加载的次数。如果加载从 0705 开始,则计数将从 0705 到 0800 等。
到目前为止,这是我必须处理的内容:
SELECT est.shift_date
,CASE est.shift_ident
WHEN '1' THEN 'D'
WHEN '2'THEN 'N'
END AS shift_ident
,min(est.start_timestamp) AS start
FROM equip_status_trans est
JOIN equip_status_code esc ON esc.status_code est.status_code
WHERE est.shift_date >= (getdate() - 180)
AND esc.status_desc 'LU Loading'
AND est.equip_ident IN (
'S803'
,'S804'
)
GROUP BY est.shift_date
,est.shift_ident
,est.equip_ident
忽略过滤器,我得到的是日期、班次标识和每个班次加载的开始时间戳。
我也有
SELECT est.shift_date
,CASE est.shift_ident
WHEN '1' THEN 'D'
WHEN '2' THEN 'N'
END AS shift_ident
,count(est.end_timestamp) AS count
,min(est.start_timestamp) AS start
FROM equip_status_trans est
JOIN equip_status_code esc ON esc.status_code = est.status_code
WHERE est.shift_date >= (getdate() - 180)
AND esc.status_desc = 'LU Loading'
AND est.equip_ident IN (
'S803'
,'S804'
)
GROUP BY est.shift_date
,est.shift_ident
,est.equip_ident
,datepart(hh, est.end_timestamp)
它给了我日期、轮班身份、加载开始时间戳以及每小时的加载计数。开始时间戳是每小时的最小加载时间戳,而不是每个班次的最小时间戳。
我遇到的问题是,每次我尝试加入两者时,它 returns 轮班期间每小时的负载计数,而不仅仅是第一个小时。
作为 SQL 服务器的新手,最简单的方法是什么?
非常感谢
这是连接语句的样子
SELECT est.shift_date,
CASE est.shift_ident
WHEN '1' THEN 'D'
WHEN '2' THEN 'N'
END as shift_ident,
min(est.start_timestamp) as start
, est.equip_ident, d1.count
FROM equip_status_trans est
join equip_status_code esc on esc.status_code = est.status_code
left outer join
(SELECT est2.shift_date,
CASE est2.shift_ident
WHEN '1' THEN 'D' WHEN '2' THEN 'N'
END as shift_ident
, count(est2.end_timestamp) as count
, min(est2.start_timestamp) as start
, est2.equip_ident
FROM equip_status_trans est2
join equip_status_code esc2 on esc2.status_code = est2.status_code
WHERE est2.shift_date >= (getdate() - 180)
and esc2.status_desc = 'LU Loading'
and est2.equip_ident IN('S803', 'S804')
GROUP BY est2.shift_date
, est2.shift_ident
, est2.equip_ident
, datepart(hh, est2.end_timestamp)
)d1 on est.start_timestamp = d1.start
WHERE est.shift_date >= (getdate() - 180)
and esc.status_desc = 'LU Loading'
and est.equip_ident IN('S803', 'S804')
GROUP BY est.shift_date, est.shift_ident, est.equip_ident, d1.count
这更多是伪SQL,它抽象出一些与连接和过滤器无关的 select 数据,但我相信您会想要这样的东西:
WITH START_TIME_PER_SHIFT AS
(/*select statement that gives you the minimum start per shift */)
SELECT EST.SHIFT_DATE,
COUNT(EST.END_TIMESTAMP) AS COUNT,
ST_TIME.START AS START
FROM EQUIP_STATUS_TRANS EST
JOIN START_TIME_PER_SHIFT ST_TIME
ON EST.SHIFT_DATE = ST_TIME.SHIFT_DATE
AND DATEPART(hh, EST.END_TIMESTAMP) =
DATEPART(hh, EST.SHIFT_DATE) --get the next hour from the shift start; only join records that are within that first hour
GROUP BY EST.SHIFT_DATE ST_TIME.START
也许这个...
WITH START_TIME_PER_SHIFT AS
(SELECT est.shift_date,
CASE est.shift_ident
WHEN '1' THEN
'D'
WHEN '2' THEN
'N'
END AS shift_ident,
/*est.equip_ident, (did you need this grouping as well?)*/
min(est.start_timestamp) AS start
FROM equip_status_trans est
JOIN equip_status_code esc
ON esc.status_code est.status_code
WHERE est.shift_date >= (getdate() - 180)
AND esc.status_desc 'LU Loading'
AND est.equip_ident IN ('S803', 'S804')
GROUP BY est.shift_date, est.shift_ident
/*,est.equip_ident*/
)
SELECT est.shift_date,
CASE est.shift_ident
WHEN '1' THEN
'D'
WHEN '2' THEN
'N'
END AS shift_ident,
/*est.equip_ident, (did you need this grouping as well?) */
count(est.end_timestamp) AS count,
st_time.start AS start
FROM equip_status_trans est
JOIN START_TIME_PER_SHIFT st_time
ON est.shift_date = st_time.shift_date
AND CASE est.shift_ident WHEN '1' THEN 'D' WHEN '2' THEN 'N' END = st_time.shift_ident
/*AND est.equip_ident = st_time.equip_ident*/
JOIN equip_status_code esc
ON esc.status_code = est.status_code
WHERE est.shift_date >= (getdate() - 180)
AND datepart(hh, est.shift_date) = datepart(hh, est.end_timestamp)
AND esc.status_desc = 'LU Loading'
AND est.equip_ident IN ('S803', 'S804')
GROUP BY est.shift_date, est.shift_ident, /*est.equip_ident,*/ st_time.start
好吧,这个可能很难解释..
我想做的是计算每个班次的第一个小时内的负载数量。我的意思是,如果加载从 0630 开始,我想计算它从 0630 到 0700 完成加载的次数。如果加载从 0705 开始,则计数将从 0705 到 0800 等。
到目前为止,这是我必须处理的内容:
SELECT est.shift_date
,CASE est.shift_ident
WHEN '1' THEN 'D'
WHEN '2'THEN 'N'
END AS shift_ident
,min(est.start_timestamp) AS start
FROM equip_status_trans est
JOIN equip_status_code esc ON esc.status_code est.status_code
WHERE est.shift_date >= (getdate() - 180)
AND esc.status_desc 'LU Loading'
AND est.equip_ident IN (
'S803'
,'S804'
)
GROUP BY est.shift_date
,est.shift_ident
,est.equip_ident
忽略过滤器,我得到的是日期、班次标识和每个班次加载的开始时间戳。
我也有
SELECT est.shift_date
,CASE est.shift_ident
WHEN '1' THEN 'D'
WHEN '2' THEN 'N'
END AS shift_ident
,count(est.end_timestamp) AS count
,min(est.start_timestamp) AS start
FROM equip_status_trans est
JOIN equip_status_code esc ON esc.status_code = est.status_code
WHERE est.shift_date >= (getdate() - 180)
AND esc.status_desc = 'LU Loading'
AND est.equip_ident IN (
'S803'
,'S804'
)
GROUP BY est.shift_date
,est.shift_ident
,est.equip_ident
,datepart(hh, est.end_timestamp)
它给了我日期、轮班身份、加载开始时间戳以及每小时的加载计数。开始时间戳是每小时的最小加载时间戳,而不是每个班次的最小时间戳。
我遇到的问题是,每次我尝试加入两者时,它 returns 轮班期间每小时的负载计数,而不仅仅是第一个小时。
作为 SQL 服务器的新手,最简单的方法是什么?
非常感谢
这是连接语句的样子
SELECT est.shift_date,
CASE est.shift_ident
WHEN '1' THEN 'D'
WHEN '2' THEN 'N'
END as shift_ident,
min(est.start_timestamp) as start
, est.equip_ident, d1.count
FROM equip_status_trans est
join equip_status_code esc on esc.status_code = est.status_code
left outer join
(SELECT est2.shift_date,
CASE est2.shift_ident
WHEN '1' THEN 'D' WHEN '2' THEN 'N'
END as shift_ident
, count(est2.end_timestamp) as count
, min(est2.start_timestamp) as start
, est2.equip_ident
FROM equip_status_trans est2
join equip_status_code esc2 on esc2.status_code = est2.status_code
WHERE est2.shift_date >= (getdate() - 180)
and esc2.status_desc = 'LU Loading'
and est2.equip_ident IN('S803', 'S804')
GROUP BY est2.shift_date
, est2.shift_ident
, est2.equip_ident
, datepart(hh, est2.end_timestamp)
)d1 on est.start_timestamp = d1.start
WHERE est.shift_date >= (getdate() - 180)
and esc.status_desc = 'LU Loading'
and est.equip_ident IN('S803', 'S804')
GROUP BY est.shift_date, est.shift_ident, est.equip_ident, d1.count
这更多是伪SQL,它抽象出一些与连接和过滤器无关的 select 数据,但我相信您会想要这样的东西:
WITH START_TIME_PER_SHIFT AS
(/*select statement that gives you the minimum start per shift */)
SELECT EST.SHIFT_DATE,
COUNT(EST.END_TIMESTAMP) AS COUNT,
ST_TIME.START AS START
FROM EQUIP_STATUS_TRANS EST
JOIN START_TIME_PER_SHIFT ST_TIME
ON EST.SHIFT_DATE = ST_TIME.SHIFT_DATE
AND DATEPART(hh, EST.END_TIMESTAMP) =
DATEPART(hh, EST.SHIFT_DATE) --get the next hour from the shift start; only join records that are within that first hour
GROUP BY EST.SHIFT_DATE ST_TIME.START
也许这个...
WITH START_TIME_PER_SHIFT AS
(SELECT est.shift_date,
CASE est.shift_ident
WHEN '1' THEN
'D'
WHEN '2' THEN
'N'
END AS shift_ident,
/*est.equip_ident, (did you need this grouping as well?)*/
min(est.start_timestamp) AS start
FROM equip_status_trans est
JOIN equip_status_code esc
ON esc.status_code est.status_code
WHERE est.shift_date >= (getdate() - 180)
AND esc.status_desc 'LU Loading'
AND est.equip_ident IN ('S803', 'S804')
GROUP BY est.shift_date, est.shift_ident
/*,est.equip_ident*/
)
SELECT est.shift_date,
CASE est.shift_ident
WHEN '1' THEN
'D'
WHEN '2' THEN
'N'
END AS shift_ident,
/*est.equip_ident, (did you need this grouping as well?) */
count(est.end_timestamp) AS count,
st_time.start AS start
FROM equip_status_trans est
JOIN START_TIME_PER_SHIFT st_time
ON est.shift_date = st_time.shift_date
AND CASE est.shift_ident WHEN '1' THEN 'D' WHEN '2' THEN 'N' END = st_time.shift_ident
/*AND est.equip_ident = st_time.equip_ident*/
JOIN equip_status_code esc
ON esc.status_code = est.status_code
WHERE est.shift_date >= (getdate() - 180)
AND datepart(hh, est.shift_date) = datepart(hh, est.end_timestamp)
AND esc.status_desc = 'LU Loading'
AND est.equip_ident IN ('S803', 'S804')
GROUP BY est.shift_date, est.shift_ident, /*est.equip_ident,*/ st_time.start