我想加入一个设备表和一个移动工作订单表,我想显示所有设备和工作时间。如果某台设备没有空闲时间,那么我想在值为null的行中显示零。这就是我下面的内容。它只给我另一张表中有反应时间的设备。
SQL Server
Select e.EquipNbr, coalesce(sum(mw.MaintTech1hours + mw.MaintTech2hours + mw.MaintTech3hours), 0) ReactiveHours
From MblEquip e
inner join MobileWorkOrder mw on
mw.EquipNbr = e.EquipNbr
and mw.DateTm between DATEADD(month, DATEDIFF(month, 0, getDate()), 0)
and DATEADD(month, DATEDIFF(month, -1, getDate()), -1)
where e.DelFlg = 0 and mw.Category = 'Reactive'
group by e.EquipNbr order by ReactiveHours Desc;
答案 0 :(得分:0)
如果您在“ where condition”中测试值,则“左外部连接”等同于“内部连接”。
尝试一下(保留SQL)
Select e.EquipNbr, coalesce(sum(isnull(mw.MaintTech1hours, 0) + isnull(mw.MaintTech2hours, 0) + isnull(mw.MaintTech3hours, 0)), 0) ReactiveHours
From MblEquip e
left outer join MobileWorkOrder mw on mw.EquipNbr = e.EquipNbr and mw.Category = 'Reactive'
and Year(mw.DateTm) =Year(getdate()) and Month(mw.DateTm) =Month(getdate())
where e.DelFlg = 0
group by e.EquipNbr
order by ReactiveHours Desc;