我想组合查询。生成报表数据。姓名,事假(短),事假(中),事假(长),旷工 分别在两个数据表内。麻烦各位帮忙看看。
select * from 
(
select StaffId,
sum(case [Type] when 11 then Hours else 0 end) as '事假(短)',
sum(case [Type] when 12 then Hours else 0 end) as '事假(中)',
sum(case [Type] when 13 then Hours else 0 end) as '事假(长)'
from myhr_TimeLeaving group by StaffId
) as t1
full join
(
select StaffId,sum(AtimeMinutes) as '旷工' from myhr_TimeAbsenteeisms group by StaffId
) as t2
on t1.StaffId=t2.StaffId执行结果
staffid staffid
NULL NULL NULL NULL 1232 61
NULL NULL NULL NULL 1236 61
NULL NULL NULL NULL 1237 0
NULL NULL NULL NULL 1240 0
NULL NULL NULL NULL 1242 0
NULL NULL NULL NULL 1245 0
1254 0 0 80 NULL NULL
1267 0 0 0 NULL NULL
1268 7 0 0 NULL NULL
1269 0 0 0 NULL NULL我想讲两个staffid合并。呈现结果为:
staffid
1232 NULL NULL NULL 61
1236 NULL NULL NULL 61
1237 NULL NULL NULL 0
1240 NULL NULL NULL 0
1242 NULL NULL NULL 0
1245 NULL NULL NULL 0
1254 0 0 80 NULL
1267 0 0 0 NULL
1268 7 0 0 NULL
1269 0 0 0 NULL
请高手指教。

解决方案 »

  1.   

    [code=SQL]
    select isnull(t1.StaffId,t2.StaffId),事假(短),事假(中),事假(长),isnull(t2.矿工,null) from 
    (
    select StaffId,
    sum(case [Type] when 11 then Hours else 0 end) as 事假(短),
    sum(case [Type] when 12 then Hours else 0 end) as 事假(中),
    sum(case [Type] when 13 then Hours else 0 end) as 事假(长)
    from myhr_TimeLeaving group by StaffId
    ) as t1
    full join
    (
    select StaffId,sum(AtimeMinutes) as 旷工 from myhr_TimeAbsenteeisms group by StaffId
    ) as t2
    on t1.StaffId=t2.StaffId
      

  2.   


    select isnull(t1.StaffId,t2.StaffId),事假(短),事假(中),事假(长),isnull(t2.矿工,null) from  
    (
    select StaffId,
    sum(case [Type] when 11 then Hours else 0 end) as 事假(短),
    sum(case [Type] when 12 then Hours else 0 end) as 事假(中),
    sum(case [Type] when 13 then Hours else 0 end) as 事假(长)
    from myhr_TimeLeaving group by StaffId
    ) as t1
    full join
    (
    select StaffId,sum(AtimeMinutes) as 旷工 from myhr_TimeAbsenteeisms group by StaffId
    ) as t2
    on t1.StaffId=t2.StaffId
      

  3.   

    select isnull(t1.StaffId,t2.StaffId),isnull(t1.事假(短),null),isnull(t1.事假(中),null),isnull(t1.事假(长),null),isnull(t2.矿工,null) from  
    (
    select StaffId,
    sum(case [Type] when 11 then Hours else 0 end) as 事假(短),
    sum(case [Type] when 12 then Hours else 0 end) as 事假(中),
    sum(case [Type] when 13 then Hours else 0 end) as 事假(长)
    from myhr_TimeLeaving group by StaffId
    ) as t1
    full join
    (
    select StaffId,sum(AtimeMinutes) as 旷工 from myhr_TimeAbsenteeisms group by StaffId
    ) as t2
    on t1.StaffId=t2.StaffId
      

  4.   

    太感谢你了发现自己一些基本的SQL语法都忘记了。