[sql]代码库
insert into tb(id,product,area,num)
select 1,'A','长沙',1 from dual union
select 2,'A','长沙',2 from dual union
select 3,'A','北京',3 from dual union
select 4,'A','北京',4 from dual union
select 5,'B','上海',1 from dual union
select 6,'B','广州',2 from dual union
select 7,'B','深圳',3 from dual;
select * from tb;
--方法1
select product 商品,sum(type) 城市数,sum(num) 数量 from (
select product ,area ,sum(num) num,1 type from tb
group by product,area
)group by product;
--方法2
select product 商品,count(distinct(area)) 城市数,sum(num) 数量 from tb group by product;
by: 发表于:2017-09-28 14:49:30 顶(0) | 踩(0) 回复
??
回复评论