求一个SQL查询写法

表如下:
表1
brand  codekey  content1
a         1             ccc
a         2             bbb
a         3             ddd
b         5             aaa
...
表2
brand  codekey  content2
a        1              xxx
a        1              yyy
a        2              zzz
b        8              hhh
...

我想查询的是,当条件brand = 'a' 时,查询结果如下:
brand  codekey  content
a         1             cccxxxyyy
a         2             bbbzzz
a         3             ddd

解释一下,返回的结果是key中不应该有重复的,对应的content列是相应的codekey的content1中的所有与content2中所有的字符串连接。


问题已解决,贴出解决办法,以供参考:

select  brand, 
       codekey, 
       
replace (path,  '   ' '' as  content 
from  (  select  brand, 
              codekey, 
              
max (SYS_CONNECT_BY_PATH(content,  '   ' ))  as  path 
       
from  (  select  brand, 
                     codekey, 
                     content,
                     (row_number() 
over ( order   by  brand, codekey, content)  +  dense_rank()  over ( order   by  brand, codekey)) rn, 
                     
min (content)  over (partition  by  brand, codekey) content1 
              
from  (  select  brand, 
                            codekey, 
                            content1 
as  content 
                     
from  table1 
                     
union   all   select  brand, 
                                      codekey, 
                                      content2 
as  content 
                               
from  table2 
                               
order   by  brand, codekey
                   )
            ) 
       start 
with  content  =  content1 
       connect 
by  prior rn  =  rn  -   1  
       
group   by  brand, codekey 
       
having  brand  =   ' a '
)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值