Sql server SQL Server时间表查询
我需要一个查询和数据来生成时间表 我目前的查询如下:Sql server SQL Server时间表查询,sql-server,tsql,Sql Server,Tsql,我需要一个查询和数据来生成时间表 我目前的查询如下: SELECT contract.lect_code code1, contract.coll_code code2, line_date, start_time, end_time, DATEPART(DW,line_date) AS day_number, datename (dw,line_date) AS nameofday, conver
SELECT contract.lect_code code1,
contract.coll_code code2,
line_date,
start_time,
end_time,
DATEPART(DW,line_date) AS day_number,
datename (dw,line_date) AS nameofday,
convert(varchar(8),start_time,108) AS start_time2,
convert(varchar(8),end_time,108) AS end_time2
FROM bk_line
INNER JOIN contract ON contract.contract_no = bk_line.contract_no
WHERE line_date BETWEEN '2013/09/23' AND '2013/09/27'
AND coll_code = 'TEL01'
AND bk_line_status = 'CE'
--And lect_code = 10430973
这为我提供了以下格式的数据:
我需要一种方法,将时间分解为每个代码1和行日期15分钟的时段
大概是这样的:
;with t as
(
select cast('00:00:00' as time) as intStart,
0 as l
union all
select DATEADD(MINUTE, 15,intStart),
l+1
from t
where intStart<='23:30:00'
)
select *
from t;
代码1 |代码2 |行|日期| 0900工作| 0915工作| 0930工作|
其中0900worked中的值为T或F
-
编辑
我需要按代码1和行日期对时段进行分组。因此,在上述示例中,1045096在2013/09/25工作了两次。我需要两个会话显示在同一行,并计算所有时段。可以使用以下内容创建一个间隔表:
select case
when convert(varchar(8),end_time,108) >= '09:15:00'
and convert(varchar(8),start_time,108) <= '09:00:00'
then 'T'
else 'F'
end as [0900worked]
, case
when convert(varchar(8),end_time,108) >= '09:30:00'
and convert(varchar(8),start_time,108) <= '09:15:00'
then 'T'
else 'F'
end as [0915worked]
, ...
;with t as
(
select cast('00:00:00' as time) as intStart,
0 as l
union all
select DATEADD(MINUTE, 15,intStart),
l+1
from t
where intStart<='23:30:00'
)
select *
from t;
然后,您可以使用它连接到您的数据,并提供查看白天15分钟间隔中任何一个间隔的范围
然后,您可以对任何间隔使用Andomar的select语句。可以使用以下内容创建间隔表:
;with t as
(
select cast('00:00:00' as time) as intStart,
0 as l
union all
select DATEADD(MINUTE, 15,intStart),
l+1
from t
where intStart<='23:30:00'
)
select *
from t;
然后,您可以使用它连接到您的数据,并提供查看白天15分钟间隔中任何一个间隔的范围
然后,您可以对任何时间间隔使用Andomar的select语句。您可以这样做,但我认为有更好的方法来检查某人是否在给定的时间间隔内工作
with "nums"
as
(
select 1 as "value"
union all select "value" + 15 as "value"
from "nums"
where "value" <= 95*15
)
, "intervals"
as
(
select
"id" = "value" / 15
, "startDate" = dateadd( minute, "value" -1 , dateadd(year, datediff(year, 0, getdate()), 0))
, "endDate" = dateadd( minute, "value" + 14, dateadd( year, datediff( year, 0, getdate()), 0 ))
from
"nums"
)
,"matched"
as
(
select
I.*
, D."id" as "code1"
from
intervals as I
left join "data" as D
on D."startDate" <= I."startDate"
and D."endDate" >= I."endDate"
)
select
*
from
(
select
"code1"
, "startDate"
from
"matched"
) as Data
pivot( count(Data."startdate")
for "startDate"
in ( "2013-01-01 00:00:00.000"
, "2013-01-01 00:15:00.000"
, "2013-01-01 00:30:00.000"
, "2013-01-01 00:45:00.000"
, "2013-01-01 01:00:00.000"
, "2013-01-01 01:15:00.000"
, "2013-01-01 01:30:00.000"
, "2013-01-01 01:45:00.000"
, "2013-01-01 02:00:00.000"
, "2013-01-01 02:15:00.000"
, "2013-01-01 02:30:00.000"
)) as p
where p.code1 is not null
你可以这样做,但我认为有更好的方法来检查某人是否在给定的时间间隔内工作
with "nums"
as
(
select 1 as "value"
union all select "value" + 15 as "value"
from "nums"
where "value" <= 95*15
)
, "intervals"
as
(
select
"id" = "value" / 15
, "startDate" = dateadd( minute, "value" -1 , dateadd(year, datediff(year, 0, getdate()), 0))
, "endDate" = dateadd( minute, "value" + 14, dateadd( year, datediff( year, 0, getdate()), 0 ))
from
"nums"
)
,"matched"
as
(
select
I.*
, D."id" as "code1"
from
intervals as I
left join "data" as D
on D."startDate" <= I."startDate"
and D."endDate" >= I."endDate"
)
select
*
from
(
select
"code1"
, "startDate"
from
"matched"
) as Data
pivot( count(Data."startdate")
for "startDate"
in ( "2013-01-01 00:00:00.000"
, "2013-01-01 00:15:00.000"
, "2013-01-01 00:30:00.000"
, "2013-01-01 00:45:00.000"
, "2013-01-01 01:00:00.000"
, "2013-01-01 01:15:00.000"
, "2013-01-01 01:30:00.000"
, "2013-01-01 01:45:00.000"
, "2013-01-01 02:00:00.000"
, "2013-01-01 02:15:00.000"
, "2013-01-01 02:30:00.000"
)) as p
where p.code1 is not null
+如果以十次间隔为准,1个点可能是一个大问题……但假设这就是beast的本质,谢谢。我已经将它添加到我的代码中,它工作得很好。我还有一个问题。我会把它作为编辑贴出来。你可以把箱子放在maxcase里。。。结束时为[0900已工作]。因为'T'>'F',如果组中的任何一行包含9:00-9:15的时间间隔,则返回true。谢谢。我把上面的内容放在一个CTE中,然后对每一个区间使用max。如果是十个区间,+1点……可能是一个大问题……但是假设这就是beast的性质谢谢。我已经将它添加到我的代码中,它工作得很好。我还有一个问题。我会把它作为编辑贴出来。你可以把箱子放在maxcase里。。。结束时为[0900已工作]。因为'T'>'F',如果组中的任何一行包含9:00-9:15的时间间隔,则返回true。谢谢。我把上面的内容放在一个CTE中,然后对每个区间使用max。Query给出了0和1,而不是T和F,但是当Query给出了0和1,而不是T和F时,你可以很容易地放一个case,但是当你在它周围时,你可以很容易地放一个case