日期:2014-05-17 浏览次数:20926 次
select b,max(decode(rn,1,a,null)) 途径1,max(decode(rn,2,a,null)) 途径2 from (
select b,a,row_number()over(partition by b order by a) rn from tmp_table) group by b
--> 测试数据:a
if object_id('a') is not null drop table a
go
create table a([药品编码] varchar(10),[药品名称] varchar(10))
insert a
select 'PCC001M','阿莫西林'
--> 测试数据:b
if object_id('b') is not null drop table b
go
create table b([药品编码] varchar(10),[给药途径] varchar(10))
insert b
select 'PCC001M','Route1' union all
select 'PCC001M','Route2'
--查询语句
select b.[药品编码],a.[药品名称],[途径一]=max(case [给药途径] when 'Route1' then [给药途径] else '' end),
[途径二]=max(case [给药途径] when 'Route2' then [给药途径] else '' end)
from b left join a on a.[药品编码]=b.[药品编码]
group by b.[药品编码],a.[药品名称]