Oracle wm_concat的使用

表结构:

姓名  科目  成绩

张三   语文    89

张三   数学   73

李四   语文   73

李四   数学   73


需要的格式:

姓名   科目成绩

张三   语文=89,数字=73

李四   语文=73,数字=73


查询语句:

select 姓名,wm_concat(to_char(科目||'='||成绩)) from 表名 where 姓名 in ('张三','李四')

* 注:wm_concat 内必须要文本字符


但在sql server里没有这个函数,那怎么办?只能另找其他方法了。

select 姓名, [科目]=stuff((select ','+[科目] from 表名 A t where A.姓名=B.姓名 for xml path('')), 1, 1, '')
   from 表名 B
 group by 姓名


排序语句:

select 姓名,wm_concat(to_char(科目||'='||成绩)) over (order by 科目 rows between unbounded preceding and unbounded following) 成绩 from 表名 where 姓名 in ('张三','李四') where rownum=1
* 注: 分组字段 为相同的行值


*over(开窗函数)的使用说明

over(order by 字段) 照字段排序
over(partition by 字段)按照字段分区

范例:
over(order by 字段 rows between 2 preceding and 4 following)每行对应的数据窗口是之前2行,之后4行
over(order by 字段 rows between unbounded preceding and unbounded following)每行对应的数据窗口是从第一行到最后一行
over(partition by null) 等级从第一行到最后一行

参考:http://zonghl8006.blog.163.com/blog/static/4528311520083995931317/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值