2015-10-15 42 views
1

讓我們試試這種方式。你可以看到,我有很多SELECT(我只顯示3個,但我有20個以上),我需要統一所有這些。每當我需要添加品牌時,我都必須逐個更改所有的SELECT。這很無聊。SQL:While和Union統一SELECTs

此外,我有超過10個品牌(不僅3),我也需要一個一個地打字。如果我可以只改變一次,並且讓WHILE LOOP爲其他SELECT執行剩餘操作,那麼這並不是那麼糟糕。但是,如果我必須更換所有SELECT的所有品牌,這需要我很多時間。

DECLARE @COUNTRY varchar(30) 
SET @COUNTRY = 'GERMANY' 

SELECT * FROM 
(SELECT * FROM 
    (SELECT 
     FOOD_CATEGORY, 
     YEAR_SALE, 
     CASE WHEN SUM(SALES)=0 THEN 0 
     ELSE ROUND(SUM(CASE WHEN FABRICANTE='BRAND A' THEN ROUND(SALES,3) ELSE 0 END)/SUM(SALES),3) 
     END AS Share_A, 
     CASE WHEN SUM(SALES)=0 THEN 0 
     ELSE ROUND(SUM(CASE WHEN FABRICANTE='BRAND B' THEN ROUND(SALES,3) ELSE 0 END)/SUM(SALES),3) 
     END AS Share_B, 
     CASE WHEN SUM(SALES)=0 THEN 0 
     ELSE ROUND(SUM(CASE WHEN FABRICANTE='BRAND C' THEN ROUND(SALES,3) ELSE 0 END)/SUM(SALES),3) 
     END AS Share_C 
    FROM tableChicken 
    WHERE 
     AREA = @COUNTRY 
    GROUP BY 
     FOOD_CATEGORY, 
     YEAR_SALE) AS CHICKEN 

UNION 

SELECT * FROM 

    (SELECT 
     FOOD_CATEGORY, 
     YEAR_SALE, 
     CASE WHEN SUM(SALES)=0 THEN 0 
     ELSE ROUND(SUM(CASE WHEN FABRICANTE='BRAND A' THEN ROUND(SALES,3) ELSE 0 END)/SUM(SALES),3) 
     END AS Share_A, 
     CASE WHEN SUM(SALES)=0 THEN 0 
     ELSE ROUND(SUM(CASE WHEN FABRICANTE='BRAND B' THEN ROUND(SALES,3) ELSE 0 END)/SUM(SALES),3) 
     END AS Share_B, 
     CASE WHEN SUM(SALES)=0 THEN 0 
     ELSE ROUND(SUM(CASE WHEN FABRICANTE='BRAND C' THEN ROUND(SALES,3) ELSE 0 END)/SUM(SALES),3) 
     END AS Share_C 
    FROM tableSwine 
    WHERE 
     AREA = @COUNTRY 
    GROUP BY 
     FOOD_CATEGORY, 
     YEAR_SALE) AS SWINE 

UNION 

SELECT * FROM 

    (SELECT 
     FOOD_CATEGORY, 
     YEAR_SALE, 
     CASE WHEN SUM(SALES)=0 THEN 0 
     ELSE ROUND(SUM(CASE WHEN FABRICANTE='BRAND A' THEN ROUND(SALES,3) ELSE 0 END)/SUM(SALES),3) 
     END AS Share_A, 
     CASE WHEN SUM(SALES)=0 THEN 0 
     ELSE ROUND(SUM(CASE WHEN FABRICANTE='BRAND B' THEN ROUND(SALES,3) ELSE 0 END)/SUM(SALES),3) 
     END AS Share_B, 
     CASE WHEN SUM(SALES)=0 THEN 0 
     ELSE ROUND(SUM(CASE WHEN FABRICANTE='BRAND C' THEN ROUND(SALES,3) ELSE 0 END)/SUM(SALES),3) 
     END AS Share_C 
    FROM tableTurkey 
    WHERE 
     AREA = @COUNTRY 
    GROUP BY 
     FOOD_CATEGORY, 
     YEAR_SALE) AS TURKEY 
) AS MAIN_BASE 

ORDER BY FOOD_CATEGORY, YEAR_SALE 

我希望現在可以理解。感謝您的幫助!

+1

你的數據庫是mysql還是sql-server? –

回答

0

我知道這是你的代碼的簡單版本,但你不能做這樣的事情。

SELECT Brand 
    , SUM(Sales_Kg) AS Sales_Volume 
FROM tblSales 
WHERE Year = 2015 
    and ID_Brand IN (1,2,3) -- ADD THIS LINE 
GROUP BY Brand 

我真的不明白,爲什麼你的複雜與CASE查詢時group by已經設置brand_id

否則,使用臨時表來保存每個結果SAMPLE

+0

謝謝胡安。我知道這樣會容易得多,但實際上我的代碼要複雜得多。 – Kreiven

+0

我使用了幾個CASE,每個品牌一個,然後選擇每個食品類別(我在一家大型食品公司工作,我們有幾個品牌和類別)。所以,我不想爲每個食品類別(雞肉,香腸,弗蘭克,火腿,冷切肉等)都輸入品牌名稱。我希望使用WHILE來選擇所有類別和品牌,而無需逐個輸入。所以,我想了解如何在這裏使用WHILE函數來解決我的問題。 – Kreiven

+0

如果你提出的問題太簡單,那就是問題所在。我的查詢和戈登完全按你的要求做。也許你需要提出另一個問題,與你有問題的其他部分的細節。嘗試閱讀[**如何創建一個最小化,完整和可驗證的示例。**](http://stackoverflow.com/help/mcve) –

0

我找到邏輯奇怪,但你可以用cross join

SELECT Brand, 
     (CASE WHEN SUM(Sales_Kg)=0 THEN 0 
      ELSE SUM(CASE WHEN ID_Brand = b.id_brand THEN Sales_Kg ELSE 0 END) 
     END) AS Sales_Volume 
FROM tblSales s CROSS JOIN 
    (SELECT 1 as id_brand UNION ALL SELECT 2 UNION ALL select 3) b 
WHERE Year = 2015 
GROUP BY Brand, b.id_brand; 

您可能還想在select中包含b.id_brand

+0

@Kreiven如果你有一個更復雜的查詢。此方法與您的存儲過程完全相同。但對於我來說,你只需計算每個品牌的總銷售額'sales_kg'。這就是Brand所設計的SUM(sales_kg)組。 –