类似于最后一个在不相等的列上连接两个表并在第二个表中累加3列

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

我想加入一个设备表和一个移动工作订单表,我想显示所有设备和工作时间。如果某台设备没有空闲时间,那么我想在值为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;
sql-server join
1个回答
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;
© www.soinside.com 2019 - 2024. All rights reserved.