我有2个查询来获取总和我希望将它们组合成单个选择查询,如果可能的话,注意它们在 where 子句中有不同的字段名称。
SELECT ISNULL(SUM(Stocking_Count), 0)
FROM Fish_Stocking
WHERE fish_id = 'B2017-1'
AND tank_id = 'H A01'
AND Date_Assigned = '2017-02-02'
AND stocking_date <= '2017-03-28'
SELECT ISNULL(SUM(NO_FISH_SWUM_OFF), 0)
FROM Swim_Thru
WHERE Dest_fish_id = 'B2017-1'
AND Dest_tank_id = 'H A01'
AND Dest_Date_Assigned = '2017-02-02'
AND date_swim_thru <= '2017-03-28'
答案 0 :(得分:0)
作为子查询
select
(SELECT ISNULL(SUM(Stocking_Count),0)
FROM Fish_Stocking
where fish_id = 'B2017-1' and tank_id = 'H A01' and
Date_Assigned = '2017-02-02' and stocking_date <= '2017-03-28') [Field1],
( SELECT ISNULL(SUM(NO_FISH_SWUM_OFF),0)
FROM Swim_Thru
where Dest_fish_id = 'B2017-1' and Dest_tank_id = 'H A01' and
Dest_Date_Assigned = '2017-02-02' and date_swim_thru <= '2017-03-28') [Field2]
答案 1 :(得分:0)
我希望您正在尝试使用UNION ALL
完成此操作。
注意:我还将ISNULL(SUM(Stocking_Count),0)
更改为SUM(ISNULL(Stocking_Count, 0))
。
SELECT SUM(Total) AS Total FROM (
(SELECT SUM(ISNULL(Stocking_Count, 0)) as Total
FROM Fish_Stocking
where fish_id = 'B2017-1' and tank_id = 'H A01' and
Date_Assigned = '2017-02-02' and stocking_date <= '2017-03-28')
UNION ALL
(SELECT SUM(ISNULL(NO_FISH_SWUM_OFF, 0)) as Total
FROM Swim_Thru
where Dest_fish_id = 'B2017-1' and Dest_tank_id = 'H A01' and
Dest_Date_Assigned = '2017-02-02' and date_swim_thru <= '2017-03-28')
) Net