SQL查询使用case和Group by aggregation

时间:2015-08-07 12:39:01

标签: sql sql-server

我有以下情况:

1我所拥有的仓库表:ProductID,数量和地址 1我所拥有的商店表:ProductID和数量 1个商店存储地址表

我需要知道的是,如果产品是“库存”或“缺货”,使用的标准是产品被视为“缺货”,则必须具有数量> = 0或>两个表(商店和仓库)中的20k

我尝试的是以下内容:

SELECT Product_Cod, qty_wh, QtyStore,
    CASE
        WHEN sum(QtyStore) BETWEEN 1 AND 20000 THEN 
                                CASE
                                        WHEN qty_wh > 0 THEN 'In Stock'
                                        WHEN qty_wh = 0 THEN 'In Stock'
                                END
        WHEN sum(QtyStore) = 0 OR sum(QtyStore) > 20000 THEN
                                CASE
                                        WHEN qty_wh <= 0 THEN 'Out of Stock'
                                        WHEN qty_wh > 0 THEN 'in Stock'
                                END
    END AS [Result]
FROM tb_Products, tb_qtyStock, tb_StoreProdAddress
WHERE Product_Cod = wh_CodProduct
AND Product_Cod = Store_CodProduct
GROUP BY Product_Cod, qty_wh, QtyStore 
ORDER BY Product_Cod

哪个输出:

   ProdCod qty_wh QtyStore   Result
    10026   26      0       In Stock
    10026   26      1       In Stock
    10026   26      2       In Stock
    10070   25      0       In Stock
    10070   25      2       In Stock
    10070   25      3       In Stock
    10071   20      0       In Stock
    10071   20      1       In Stock
    10071   20      29991   In Stock
    10072   32      0       In Stock
    10072   32      1       In Stock
    10072   32      29978   In Stock
    10204   0       0       Out of Stock
    10204   0       1       In Stock
    10204   0       4       In Stock
    10204   0       29996   Out of Stock

但我不能让它像我想的那样工作,因为QtyStore列不是SUM() 它重复,因为tb_StoreProdAddress用于多个商店,因此多个商店可以拥有相同的产品,但每个商店可能具有该产品的不同地址,但仓库中给定产品的地址是相同的。

有一种方法来SUM()QtyStore列,只需将qty_wh组分组

3 个答案:

答案 0 :(得分:1)

您可以尝试最大化qty_wth,然后像这样总结QtyStore:

SELECT Product_Cod, max(qty_wh) as qty_wh, sum(QtyStore) as QtyStore,
    CASE
        WHEN sum(QtyStore) BETWEEN 1 AND 20000 THEN 
                            CASE
                                    WHEN qty_wh > 0 THEN 'In Stock'
                                    WHEN qty_wh = 0 THEN 'In Stock'
                            END
        WHEN sum(QtyStore) = 0 OR sum(QtyStore) > 20000 THEN
                            CASE
                                    WHEN qty_wh <= 0 THEN 'Out of Stock'
                                    WHEN qty_wh > 0 THEN 'in Stock'
                            END
END AS [Result]
FROM tb_Products, tb_qtyStock, tb_StoreProdAddress
WHERE Product_Cod = wh_CodProduct
AND Product_Cod = Store_CodProduct
GROUP BY Product_Cod
ORDER BY Product_Cod

答案 1 :(得分:1)

为了避免多次进行相同的操作,你可以在子查询中进行计算:

SELECT Product_Cod,
       qty_wh,
       QtyStore,
       CASE
          WHEN QtyStore BETWEEN 1 AND 20000
          THEN
             CASE
                WHEN qty_wh > 0 THEN 'In Stock'
                WHEN qty_wh = 0 THEN 'In Stock'
             END
          WHEN QtyStore = 0 OR QtyStore > 20000
          THEN
             CASE
                WHEN qty_wh <= 0 THEN 'Out of Stock'
                WHEN qty_wh > 0 THEN 'in Stock'
             END
       END AS [Result]
    FROM (SELECT Product_Cod,
               MAX (qty_wh) AS qty_wh,
               SUM (QtyStore) AS QtyStore,
               FROM tb_Products,
               tb_qtyStock,
               tb_StoreProdAddress 
             WHERE Product_Cod = wh_CodProduct
             AND Product_Cod = Store_CodProduct
             GROUP BY Product_Cod)
    ORDER BY Product_Cod

我希望它有所帮助。

答案 2 :(得分:0)

只需从GROUP BY子句中删除QtyStore列,然后将SUM(QtyStore)放入查询的SELECT部分。

SELECT Product_Cod, qty_wh, SUM(QtyStore) AS TotalQtyStore,
    CASE
        WHEN sum(QtyStore) BETWEEN 1 AND 20000 THEN 
                                CASE
                                        WHEN qty_wh > 0 THEN 'In Stock'
                                        WHEN qty_wh = 0 THEN 'In Stock'
                                END
        WHEN sum(QtyStore) = 0 OR sum(QtyStore) > 20000 THEN
                                CASE
                                        WHEN qty_wh <= 0 THEN 'Out of Stock'
                                        WHEN qty_wh > 0 THEN 'in Stock'
                                END
    END AS [Result]
FROM tb_Products, tb_qtyStock, tb_StoreProdAddress
WHERE Product_Cod = wh_CodProduct
AND Product_Cod = Store_CodProduct
GROUP BY Product_Cod, qty_wh
ORDER BY Product_Cod