求教 SQL处理方法

[复制链接]
查看11 | 回复4 | 2011-3-28 10:31:54 | 显示全部楼层 |阅读模式
A表
姓名
品种
数量
张三
可乐
1
张三
百事
2
张三
雪碧
3
李四
可乐
8
陈五
雪碧
10
如何把表的结果改成
姓名
雪碧
可乐
百事
张三
3
1
2
这样的形式,最好是存储过程处理,请高人赐教
如果像李四这样只有可乐有数量的话,其他两个品种赋NULL值

回复

使用道具 举报

千问 | 2011-3-28 10:31:54 | 显示全部楼层
select * from (select 姓名,雪碧=sum(case when 品种='雪碧'then 数量 else '0' end),可乐=sum(case when 品种='可乐' then 数量 else '0' end),百事=sum(case when 品种='百事' then 数量 else '0' end)from Agroup by 姓名)as AA
回复

使用道具 举报

千问 | 2011-3-28 10:31:54 | 显示全部楼层
select 姓名,sum(case 品种 = '雪碧' then 数量else 0 end) 雪碧, sum(case when 品种= '可乐' then 数量else 0 end) 可乐,sum(case when 品种= '百事' then 数量else 0 end) 百事, from 表A group by 姓名;
回复

使用道具 举报

千问 | 2011-3-28 10:31:54 | 显示全部楼层
select name,
sum(decode(type,'可乐',count,0)) Cola,
sum(decode(type,'雪碧',count,0)) Sprite,
sum(decode(type,'百事',count,0)) pepsifrom Agroup by name注 name
回复

使用道具 举报

千问 | 2011-3-28 10:31:54 | 显示全部楼层
select 姓名,case 品种 when '雪碧' then s else 'NULL' end ,case 品种 when '可乐then s else 'NULL' end ,case 品种 when '百事then s else 'NULL' end ,(select 姓名,品种,sum(数量) sfrom agro
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行