mysql 列交换_数据库行列互换

1:SQL Server : case group 2:oracle decode group 假设用到的sql语句为: SELECT [姓名],[时代],[金钱] FROM [test].[dbo].[people] 想要等到如下的结果: 姓名 年轻 中年 老年 张三 1000 5000 800 李四 1200 6000 500 SQL Server: case [时代] when '年轻'

1:SQL Server : case group

2:oracle decode group

假设用到的sql语句为:

SELECT [姓名],[时代],[金钱]

FROM [test].[dbo].[people]

test.jsp?url=http%3A%2F%2Fimg.ddvip.com%2F2009_08_29%2F1251557928_ddvip_7705.jpg&refer=http%3A%2F%2Fblog.csdn.net%2Flosing_coder%2Farticle%2Fdetails%2F7344590想要等到如下的结果:

姓名

年轻

中年

老年

张三

1000

5000

800

李四

1200

6000

500

SQL Server:

case  [时代] when '年轻' then [金钱] else 0 end as 年轻

case when  [时代]= '年轻' then [金钱] else 0 end as 年轻

select [姓名],sum([年轻]) as 年轻,sum([中年]) as 中年,sum([老年]) as 老年 from

(SELECT [姓名],[时代],[金钱],

case  [时代] when '年轻' then [金钱] else 0 end as 年轻,

case  [时代] when '中年' then [金钱] else 0 end as 中年,

case  [时代] when '老年' then [金钱] else 0 end as 老年

FROM [test].[dbo].[people]) t

group by [姓名]

Oracle : decode(时代,'年轻',金钱,0)) 年轻

select [姓名], max(decode([时代],年轻,金钱,0))as 年轻, max(decode([时代],中年,金钱,0))as 中年, max(decode([时代],老年,金钱,0))as 老年 from [test].[dbo].[people] group by 姓名.

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值