日期:2014-05-17 浏览次数:21008 次
--> 测试数据:@A表
declare @A表 table([品种] varchar(4),[价格] numeric(2,1))
insert @A表
select '苹果',3.5 union all
select '李子',2.5 union all
select '樱桃',3.5 union all
select '西瓜',1.5 union all
select '核桃',2.5
--> 测试数据:@B表
declare @B表 table([客户] varchar(5),[品种] varchar(4),[重量] int)
insert @B表
select '客户1','苹果',3 union all
select '客户2','李子',2 union all
select '客户3','苹果',5 union all
select '客户4','樱桃',2 union all
select '客户5','樱桃',2 union all
select '客户6','核桃',2
select
a.价格,LTRIM(COUNT(1))+'人' AS 购买人数
from @B表 b LEFT JOIN @A表 a ON b.品种=a.品种
GROUP BY a.价格
/*
价格 购买人数
------------------------------ --------------
2.5 2人
3.5 4人
*/