到目前为止,我尝试用max和min减去这是正常的,除非有第三条记录,其中包含0。我还在一个案例陈述中试图寻找枯萎的预测或工作,并将其中的一个设置为否定,但这也不起作用。我需要按员工,rectype,datebegin和dateend分组的差异。 0 recs可以进入那里
Table
Employee RecType DateBegin DateEnd Hours
Sample
123,Forecast,1/1/2014,1/8/2014,5
123,Forecast,1/9/2014,1/16/2014,7
123,Forecast,1/9/2014,1/16/2014,0
123,Worked,1/1/2014,1/8/2014,5
123,Worked,1/9/2014,1/16/2014,4
Output I'm looking for
123,Difference,1/1/2014,1/8/2014,0
123,Difference,1/9/2014,1/16/2014,3
答案 0 :(得分:0)
假设您将拥有Employee,RecType,DateBegin,DateEnd的匹配记录 对于预测和工作,您可以通过以下方式获得结果:
Set DateFormat mdy
declare @a table(Employee int ,RecType varchar(20), DateBegin DateTime, DateEnd dateTime, Hours float)
insert into @a
select 123,'Forecast','1/1/2014','1/8/2014',5
UNION
select 123,'Forecast','1/9/2014','1/16/2014',7
UNION
select 123,'Forecast','1/9/2014','1/16/2014',0
UNION
select 123,'Worked','1/1/2014','1/8/2014',5
UNION
select 123,'Worked','1/9/2014','1/16/2014',4
Select x.Employee,x.DateBegin,x.DateEnd,x.Hours-y.Hours as Hours
from
(
Select Employee,DateBegin,DateEnd,SUM(Hours) as Hours
from @a where RecType='Forecast'
Group by Employee,DateBegin,DateEnd
) x
JOIN
(
Select Employee,DateBegin,DateEnd,SUM(Hours) as Hours
from @a where RecType='Worked'
Group by Employee,DateBegin,DateEnd
) y
on x.Employee=y.Employee and x.DateBegin=y.DateBegin and x.DateEnd=y.DateEnd