2017-03-28 99 views
1

我有2個查詢得到總和,我想將它們合併成一個單一的選擇查詢如果可能的話指出他們在那裏條款不同的字段名。從2 SQL返回單金額查詢

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' 
+1

提示:'UNION ALL'。 –

+0

你可以檢查我的下面的工作答案.. – Aruna

回答

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] 
+0

也可以工作,但在2個領域的代碼返回,所以再次非常感謝你的幫助。 –

+0

是的,因爲我認爲你是在1行後的兩個結果。 – maSTAShuFu

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 
+0

非常感謝,非常感謝您的幫助。 –

+0

不客氣:-) – Aruna

+0

如果你很高興,那麼請接受答案 – Aruna