SQL181. 某宝店铺的实际销售额与客单价
描述
sales_date | user_id | item_id | sales_num | sales_price |
2021-11-01 | 1 | A001 | 1 | 90 |
2021-11-01 | 2 | A002 | 2 | 220 |
2021-11-01 | 2 | B001 | 1 | 120 |
2021-11-02 | 3 | C001 | 2 | 500 |
2021-11-02 | 4 | B001 | 1 | 120 |
2021-11-03 | 5 | C001 | 1 | 240 |
2021-11-03 | 6 | C002 | 1 | 270 |
2021-11-04 | 7 | A003 | 1 | 180 |
2021-11-04 | 8 | B002 | 1 | 140 |
2021-11-04 | 9 | B001 | 1 | 125 |
2021-11-05 | 10 | B003 | 1 | 120 |
2021-11-05 | 10 | B004 | 1 | 150 |
2021-11-05 | 10 | A003 | 1 | 180 |
2021-11-06 | 11 | B003 | 1 | 120 |
2021-11-06 | 10 | B004 | 1 | 150 |
sales_total | per_trans |
2725 | 247.73 |
示例1
输入:
drop table if exists sales_tb; CREATE TABLE sales_tb( sales_date date NOT NULL, user_id int(10) NOT NULL, item_id char(10) NOT NULL, sales_num int(10) NOT NULL, sales_price int(10) NOT NULL ); INSERT INTO sales_tb VALUES('2021-11-1', 1, 'A001', 1, 90); INSERT INTO sales_tb VALUES('2021-11-1', 2, 'A002', 2, 220); INSERT INTO sales_tb VALUES('2021-11-1', 2, 'B001', 1, 120); INSERT INTO sales_tb VALUES('2021-11-2', 3, 'C001', 2, 500); INSERT INTO sales_tb VALUES('2021-11-2', 4, 'B001', 1, 120); INSERT INTO sales_tb VALUES('2021-11-3', 5, 'C001', 1, 240); INSERT INTO sales_tb VALUES('2021-11-3', 6, 'C002', 1, 270); INSERT INTO sales_tb VALUES('2021-11-4', 7, 'A003', 1, 180); INSERT INTO sales_tb VALUES('2021-11-4', 8, 'B002', 1, 140); INSERT INTO sales_tb VALUES('2021-11-4', 9, 'B001', 1, 125); INSERT INTO sales_tb VALUES('2021-11-5', 10, 'B003', 1, 120); INSERT INTO sales_tb VALUES('2021-11-5', 10, 'B004', 1, 150); INSERT INTO sales_tb VALUES('2021-11-5', 10, 'A003', 1, 180); INSERT INTO sales_tb VALUES('2021-11-6', 11, 'B003', 1, 120); INSERT INTO sales_tb VALUES('2021-11-6', 10, 'B004', 1, 150);
输出:
2725|247.73
Mysql 解法, 执行用时: 36ms, 内存消耗: 6404KB, 提交时间: 2021-12-18
select sum(sales_price) as sales_total,round(sum(sales_price)/count(distinct user_id),2) as per_trans from sales_tb
Mysql 解法, 执行用时: 36ms, 内存消耗: 6500KB, 提交时间: 2022-01-25
select sum(sales_price) as sales_total, round(sum(sales_price)/count(distinct user_id),2) as per_trans from sales_tb
Mysql 解法, 执行用时: 37ms, 内存消耗: 6376KB, 提交时间: 2021-12-12
select sum(sales_price) sales_total, round(sum(sales_price)/count(distinct user_id),2) per_trans from sales_tb
Mysql 解法, 执行用时: 37ms, 内存消耗: 6380KB, 提交时间: 2021-12-21
SELECT sum(sales_price) as sales_total ,round(sum(sales_price)/count(distinct user_id),2) as per_trans FROM sales_tb
Mysql 解法, 执行用时: 37ms, 内存消耗: 6404KB, 提交时间: 2022-01-24
SELECT SUM(sales_price) AS sales_total,ROUND(SUM(sales_price)/COUNT(DISTINCT user_id),2) AS per_trans FROM sales_tb;