SQL:While和Union统一SELECT

时间:2015-10-15 22:07:03

标签: mysql sql sql-server while-loop union

让我们这样试试。

正如你所看到的,我有很多SELECT(我只有3个,但我有超过20个),我需要统一所有这些。每当我需要添加品牌时,我必须逐个更改所有SELECT。这很无聊。

此外,我有超过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

我希望现在可以理解。谢谢你的帮助!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;

您可能也想在b.id_brand中添加select