我需要获得效率数据:
create table #tbl( userid int,workid int, workstartday int, workendday int, workyear int)
and data
insert into #tbl(userid, workid, workstartday,workendday,workyear)
values(1,1,2,5,2017),(1,2,3,5,2017),(1,3,4,8,2017)
开始日和结束日是内部工作日
需要输出,显示用户多天的效率
即,
user 1 - 1st day - 0 work
user 1 - 2nd day - 1 work
user 1 - 3rd day - 2 work
user 1 - 4th day - 3 work
user 1 - 5th day - 3 work
user 1 - 6th day - 1 work
user 1 - 7th day - 1 work
提前感谢您的帮助
答案 0 :(得分:1)
首先创建一个包含所有日期的临时表。比计算每天的作品。
;with days_rte as
(
select min(workstartday) [day],max(workendday) [lastDay]
from #tbl
union all
select a.[day]+1, a.lastDay
from days_rte a
where [day] < a.lastDay
)
select t.userid,d.[day],count(t.workid) [work]
from days_rte d
join #tbl t on d.[day] between t.workstartday and t.workendday
group by t.userid,d.[day]