我有两个表
A表(品种价格表)
品种 价格(元)
苹果 3.5
李子 2.5
樱桃 3.5
西瓜 1.5
核桃 2.5
........
B表(客户购买记录)
客户 品种 重量(斤)
客户1 苹果 3
客户2 李子 2
客户3 苹果 5
客户4 樱桃 2
客户5 樱桃 2
客户6 核桃 2
................
[size=14px]我现在要想统计各价格位的购买人数
结果是
2.5元 购买人数 2人
3.5元 购买人数 3人
......
请教高手统计循环语句应该如何写~!!!![/size]
------解决方案--------------------
- SQL code
--> 测试数据:@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人 */