我是SQL数据库的新手,我有以下表格和列
productInformation
产品
soldProducts
我正在努力寻找此次促销数量和价格最高的销售产品。
根据这些表,我如何在SQL中执行此查询?
答案 0 :(得分:0)
我假设您的" soldProducts"表是单个数量。在这里( in postgresql ):
选择总量最高的产品
然后,一系列两个连接来计算总销售价格。
SELECT max.productID, max.total_quant , p.price*max.total_quant AS total_sale FROM
(SELECT productID, COUNT(*) as total_quant FROM soldProducts
GROUP BY productID HAVING COUNT(*) =
(SELECT COUNT(*) as total_quant FROM soldProducts GROUP BY productID ORDER BY total_quant DESC LIMIT 1)) as max
JOIN products AS p
ON max.productID = p.productid
JOIN productInformation AS pi
ON p.productInformationID = pi.productInformationID ;