当前位置:网站首页>SQL learning group by multi table grouping scenario

SQL learning group by multi table grouping scenario

2022-07-05 09:27:00 Xu Jiajia 233

scene

There are currently two tables :
Commodity sales history table 、 Commodity information table
The results that need to be output :
Summary of the quantity of goods currently sold .

Example

CREATE TABLE SellCheck (
goods_id string,
sell_number int
);

INSERT INTO SellCheck VALUES
("111",3),
("222",6),
("111",4),
("111",5),
("222",2);

CREATE TABLE GoodsList (
id string,
name string
);

INSERT INTO GoodsList VALUES
("111","AAA"),
("222","BBB");

select Source code

select GoodsList.name as goods_name,sum(SellCheck.sell_number) as goods_sum
from SellCheck
left outer join GoodsList
on GoodsList.id=SellCheck.goods_id
group by goods_name;

Output results

AAA|12
BBB|8

原网站

版权声明
本文为[Xu Jiajia 233]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140538084930.html