将计算机价格降低20%,同时提供超过4种产品的所有产品

时间:2017-12-13 21:53:19

标签: sql-server

我需要“将计算机价格降低20%,并且所有超过4种产品的产品”

我有以下查询,数学出错但与输出类似。

SELECT 
    o.supp_id, p.prod_id, 
    CAST(ROUND(SUM(o.price * .80), 3) AS FLOAT) as 'Price', 
    o.Quantity
FROM
    tb_product p, tb_offers o
WHERE 
    p.name = 'computer'
    AND p.prod_ID = o.prod_ID
GROUP BY
    o.supp_id, p.prod_ID, quantity

UNION

SELECT 
    supp_id, p.prod_id, 
    CAST(ROUND(SUM(tb_offers.price * .80), 3) AS FLOAT) as 'Price', 
    Quantity
FROM 
    tb_product p, tb_offers
WHERE 
    tb_offers.prod_id = p.prod_ID
GROUP BY 
    tb_offers.supp_id, p.prod_id, quantity
HAVING 
    COUNT(p.prod_Id) > 4

联合上方的查询可以解决,但是底部的查询不返回任何内容(应该是具有超过4个商品的产品。

底部查询是否向我显示了超过4个商品的产品?

我有以下表格:

Tb_Consumer - Name, City, Con_ID 
Tb_Supplier - Name, City, Supp_ID 
Tb_Offers - Name, Quantity, Prod_ID, Supp_ID 
Tb_Product - Name, Prod_ID 
Tb_Requests - Name, Quantity, Prod_ID, Con_ID 
Tb_Transactions- Tran_ID, Supp_ID, Con_ID, Quantity, Price

1 个答案:

答案 0 :(得分:0)

如果您只是在改变价格,为什么要总结结果呢?我认为你需要做一些像....

SELECT    o.supp_id
        , p.prod_id
        , CAST( (o.price * 0.80)   AS DECIMAL(18,3)) as [Price]
        , o.Quantity
FROM tb_product p
INNER JOIN  tb_offers o ON p.prod_ID = o.prod_ID
WHERE p.name = 'computer'
OR EXISTS ( SELECT 1 
            FROM tb_product p1
            WHERE p.prod_ID = p1.prod_ID
            HAVING COUNT(p1.prod_Id) > 4
           )