一条Sql语句连接多个数据

 原文传送门:http://topic.csdn.net/u/20091028/10/f2012eb6-36b7-4a19-b8ef-52ce95401a1d.html

现有表
A:
taid
1
2
3
...

B:
tbid taid seq_no code
1    1    1    AAA 
2    1    2    BBB
3    1    3    CCC
4    2    1    XXX
5    2    2    YYY

B.taid reference A.taid
如何通过sql语句得到 试图C
taid codestr
1    AAA-BBB-CCC
2    XXX-YYY

Oracle 10g

  1. select a.taid,replace(wm_concat(b.code),',','-')codestr   
  2. from a,b   
  3. where a.taid=b.taid   
  4. group by a.taid  

 

Oracle 9i

  1. select taid,substr(max(sys_connect_by_path(code,'-')),2)codestr   
  2. from(   
  3.   select a.taid,b.code,   
  4.     row_number()over(partition by a.taid order by b.tbid)rn   
  5.   from a,b   
  6.   where a.taid=b.taid)   
  7. connect by prior taid=taid and prior rn=rn-1   
  8. start with rn=1   
  9. group by taid   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值