使用完全外部联接时,不能在主查询中指定where子句

问题描述 投票:-1回答:1

我需要汇总每个时期的津贴每月津贴和旧车津贴

当我在子查询的主查询结果中指定where子句时不显示

enter image description here

enter image description here

这是我的脚本

select isnull(T1.Emp_Id,T2.Emp_Id) Emp_Id,isnull(T1.Track,T2.Track) DayTY
   ,isnull(Pay,0) MealAllowance_Mont, isnull(MealAllowance,0) MealAllowance
   ,isnull(Pay,0) + isnull(MealAllowance,0) Tot, isnull(t1.Period,T2.Period) Period
from [dbo].[SPCM_TX_MonthlyAllowance] T1
full outer join
(
   select T2.Emp_Id, T2.Track,T2.Period,sum(MealAllowance_OT) MealAllowance
   from [dbo].[SPCM_Cal_OTLog] T2
    where LEFT(CONVERT(varchar, T2.Period,23),7) = '2019-11'    
   group by T2.Emp_Id,T2.Track,T2.Period
) T2
   on T1.Emp_Id = T2.Emp_Id   
   order by Period desc

我的问题是我不能同时指定两个表的周期

sql sql-server
1个回答
0
投票
我建议union allgroup by。我不太清楚结果集需要什么,但是像这样:

select t.Emp_Id, t.Track, t.period, sum(pay), sum(MealAllowance_OT), sum(MealAllowance) from ((select empid, track, period, Pay, MealAllowance_OT, MealAllowance from [dbo].[SPCM_TX_MonthlyAllowance] T1 ) union all (select Emp_Id, Track, Period, 0, MealAllowance_OT, MealAllowance from [dbo].[SPCM_Cal_OTLog] ) ) t where period >= '2019-11-01' and period < '2019-12-01' group by t.Emp_Id, t.Track, t.period

© www.soinside.com 2019 - 2024. All rights reserved.