题解 | #每个商品的销售总额#
每个商品的销售总额
https://www.nowcoder.com/practice/6d796e885ee44a9cb599f47b16a02ea4
SELECT product_name, total_sales, RANK() OVER (PARTITION BY category ORDER BY total_sales DESC, product_id ASC) AS category_rank FROM ( SELECT p.name AS product_name, SUM(o.quantity) AS total_sales, p.category, p.product_id FROM products AS p JOIN orders AS o ON p.product_id = o.product_id GROUP BY p.name, p.category, p.product_id ) AS aggregated_data ORDER BY category ASC, total_sales DESC 嗯。。。这是gpt帮忙写的 在这个过程中我遇到了一些问题 她说sum聚合函数和窗口函数row不能同时用 非要写个子查询 还说select后面的列名 除了聚合的一列 其她的都要在group by后面出现 咱也不知道是真是假 基础不好 那以后就这么用吧