SQL:每个产品销售最多,接近但不完整的城市

时间:2017-11-20 05:19:00

标签: sql-server

我必须找到每个产品最畅销的城市。 我觉得我很接近,但我很难接受下一步的尝试。我现在已经做了大约8个小时的工作(甚至没有夸大其词)。

有这些表格:

Tb_Product (prod_id, name, MU)
Tb_Supplier (supp_id, name, city)
Tb_Consumer (con_id, name, city)
Tb_Requests (quantity, prod_id, price, con_id)
Tb_Offers (quantity, prod_id, price, supp_id)
Tb_Transactions (quantity, price, prod_id, supp_id, con_id, tran_id)

到目前为止,我有以下内容:

SELECT DISTINCT Tb_Product.Name, Tb_Supplier.City, SUM(Quantity) AS 'Quantity Sold'
FROM Tb_Supplier, Tb_Transactions, Tb_Consumer, Tb_Product
WHERE Tb_Supplier.Supp_ID = Tb_Transactions.Supp_ID
AND Tb_Product.Prod_ID = Tb_Transactions.Prod_ID
GROUP BY Tb_Product.Name, Tb_Supplier.Supp_ID, Quantity, Tb_supplier.City
HAVING SUM(Quantity) >= ALL(SELECT SUM(Quantity)
                        FROM Tb_Supplier, Tb_Transactions
                        WHERE Tb_Transactions.Supp_ID=Tb_Supplier.Supp_ID
                        GROUP BY Quantity)

我得到的输出是这样的:

Name                           City                           Quantity Sold
------------------------------ ------------------------------ ----------------------
Airplane                       Madison                        3900000
Auto                           Madison                        3900000
Computer                       Madison                        3900000
Milk                           Madison                        3900000
Oil                            Madison                        3900000
Orange                         Madison                        3900000
Truck                          Madison                        3900000
TV                             Madison                        3900000

显然,销售的数量并不正确,我认为它与SUM(数量)有关,但无法确定要改变的内容。

2 个答案:

答案 0 :(得分:0)

在这种情况下,JOIN非常有用WHERE

SELECT p.Name, s.City, SUM(t.Quantity) AS 'Quantity Sold'
FROM Tb_Supplier s
JOIN Tb_Transactions t ON t.Supp_ID = s.Supp_ID
JOIN Tb_Consumer c ON c.con_id = t.con_id -- I think you forgot this condition
JOIN Tb_Product p ON p.Prod_ID = t.Prod_ID
GROUP BY p.Name, s.Supp_ID, s.City
HAVING SUM(t.Quantity) >= ALL(
            SELECT SUM(Quantity)
            FROM Tb_Transactions
            GROUP BY Supp_ID,prod_id -- check it
          )

我认为Quantity

中不需要GROUP BY
GROUP BY Tb_Product.Name, Tb_Supplier.Supp_ID, /*Quantity,*/ Tb_supplier.City

尝试以下查询

SELECT
  p.name,
  d.city,
  d.QSold
FROM
  (
    SELECT t.prod_id,s.city,SUM(t.Quantity) QSold
    FROM Tb_Transactions t
    JOIN Tb_Supplier s ON s.supp_id=t.supp_id
    GROUP BY t.prod_id,s.city
  ) d
JOIN
  (
    SELECT prod_id,MAX(QSold) MaxQSold
    FROM
      (
        SELECT t.prod_id,s.city,SUM(t.Quantity) QSold
        FROM Tb_Transactions t
        JOIN Tb_Supplier s ON s.supp_id=t.supp_id
        GROUP BY t.prod_id,s.city
      ) d
    GROUP BY prod_id
  ) m
ON d.prod_id=m.prod_id AND d.QSold=m.MaxQSold
JOIN Tb_Product p ON d.prod_id=p.prod_id

如果您的SQLServer版本支持窗口函数

,则为此查询
SELECT name,city,QSold
FROM
  (
    SELECT t.prod_id,p.name,s.city,SUM(t.Quantity) QSold,
      MAX(SUM(t.Quantity))OVER(PARTITION BY t.prod_id) MaxQSold
    FROM Tb_Transactions t
    JOIN Tb_Supplier s ON s.supp_id=t.supp_id
    JOIN Tb_Product p ON t.prod_id=p.prod_id
    GROUP BY t.prod_id,p.name,s.city
  ) q
WHERE QSold=MaxQSold

答案 1 :(得分:0)

" 每个产品已售出的城市"指的是消费城市,因此您需要将交易加入消费者,并按该城市和产品的数量加总。到达" "我们需要一个排名,所以dense_rank()可以按数量的总和(降序)排序,等级1等于"最畅销的" (注意:使用dense_rank())允许相等的第一个位置。

SELECT
      d.city
    , p.name
    , d.sum_qty
FROM (
      SELECT
            c.city
          , SUM(t.quantity) sum_qty
          , t.prod_id
          , DENSE_RANK() OVER (PARTITION BY c.city, t.prod_id ORDER BY SUM(t.quantity) DESC) rnk
      FROM Tb_Transactions t
      JOIN Tb_Consumer c ON t.con_id = c.con_id
) d
JOIN Tb_Product p ON d.Prod_ID = p.Prod_ID
WHERE d.rnk = 1
ORDER BY
      d.city
    , p.name

将子查询结果连接到product表会减少该连接中涉及的行。

nb:使用dense_rank还允许&#34; top n per&#34;结果,例如将where子句更改为where rnk <= 5将列出每个城市的前5个销售产品。