select case rn when 1 then [姓名] else '' end as 姓名, [去过的地方],b.* from ( select 姓名,去过的地方,单号,row_number() over (partition by 姓名 order by 去过的地方) rn from table_name)a join tb2 b on a.单号 = b.单号
行列转化 如果是2005以上的话,可以使用unpivot
use Tempdb go --> -->
if not object_id(N'Tempdb..#TB1') is null drop table #TB1 Go Create table #TB1([姓名] nvarchar(2),[去过的地方] nvarchar(2),[单号] int) Insert #TB1 select N'曹操',N'北京',1 union all select N'曹操',N'上海',1 union all select N'刘备',N'中山',2 union all select N'刘备',N'上海',2 union all select N'刘备',N'日本',2 union all select N'曹操',N'日本',1 Go if not object_id(N'Tempdb..#TB2') is null drop table #TB2 Go Create table #TB2([单号] int,[去的时间] int) Insert #TB2 select 1,'1980' union all select 2,'2006' Go select [姓名]=case when row=1 then [姓名] else '' end ,[去过的地方],[去的时间] from (Select [姓名],[去过的地方],[去的时间],row_number()over( partition by [姓名] order by [姓名]) as row from #TB1 as a inner join #TB2 as b on a.[单号]=b.[单号] )t /* 姓名 去过的地方 去的时间 曹操 北京 1980 上海 1980 日本 1980 刘备 中山 2006 上海 2006 日本 2006 */
select case rn when 1 then [姓名] else '' end as 姓名, [去过的地方],b.* from ( select 姓名,去过的地方,单号,row_number() over (partition by 姓名 order by 去过的地方) rn from table_name)a inner join tb2 b on a.单号 = b.单号
本帖最后由 roy_88 于 2011-07-22 16:09:11 编辑
use Tempdb go --> -->
if not object_id(N'Tempdb..#TB1') is null drop table #TB1 Go Create table #TB1([姓名] nvarchar(2),[去过的地方] nvarchar(2),[单号] int) Insert #TB1 select N'曹操',N'北京',1 union all select N'曹操',N'上海',1 union all select N'刘备',N'中山',2 union all select N'刘备',N'上海',2 union all select N'刘备',N'日本',2 union all select N'曹操',N'日本',1 Go if not object_id(N'Tempdb..#TB2') is null drop table #TB2 Go Create table #TB2([单号] int,[去的时间] int) Insert #TB2 select 1,'1980' union all select 2,'2006' Goselect case rn when 1 then [姓名] else '' end as 姓名, [去过的地方],b.* from ( select 姓名,去过的地方,单号,row_number() over (partition by 姓名 order by 去过的地方) rn from tb1)a inner join tb2 b on a.单号 = b.单号 没有东西出来。
Create table TB1([姓名] nvarchar(2),[去过的地方] nvarchar(2),[单号] int) Insert TB1 select N'曹操',N'北京',1 union all select N'曹操',N'上海',1 union all select N'刘备',N'中山',2 union all select N'刘备',N'上海',2 union all select N'刘备',N'日本',2 union all select N'曹操',N'日本',1Create table TB2([姓名] nvarchar(2),[去的时间] int) Insert TB2 select N'曹操','1980' union all select N'刘备','2006'select (case when 去过的地方=(select min(去过的地方) from tb1 a where a.姓名=tb1.姓名) then tb1.姓名 else '' end ) as 姓名, tb1.去过的地方,tb2.去的时间 from tb1,tb2 where tb1.姓名=tb2.姓名/* 姓名 去过的地方 去的时间 ---- ----- ----------- 曹操 北京 1980 上海 1980 中山 2006 上海 2006 刘备 日本 2006 日本 1980 */
select case rn when 1 then [姓名] else '' end as 姓名,
[去过的地方],b.*
from
( select 姓名,去过的地方,单号,row_number() over (partition by 姓名 order by 去过的地方) rn from table_name)a
join tb2 b on a.单号 = b.单号
如果是2005以上的话,可以使用unpivot
go
--> -->
if not object_id(N'Tempdb..#TB1') is null
drop table #TB1
Go
Create table #TB1([姓名] nvarchar(2),[去过的地方] nvarchar(2),[单号] int)
Insert #TB1
select N'曹操',N'北京',1 union all
select N'曹操',N'上海',1 union all
select N'刘备',N'中山',2 union all
select N'刘备',N'上海',2 union all
select N'刘备',N'日本',2 union all
select N'曹操',N'日本',1
Go
if not object_id(N'Tempdb..#TB2') is null
drop table #TB2
Go
Create table #TB2([单号] int,[去的时间] int)
Insert #TB2
select 1,'1980' union all
select 2,'2006'
Go
select
[姓名]=case when row=1 then [姓名] else '' end
,[去过的地方],[去的时间]
from (Select
[姓名],[去过的地方],[去的时间],row_number()over( partition by [姓名] order by [姓名]) as row
from #TB1 as a
inner join #TB2 as b on a.[单号]=b.[单号]
)t
/*
姓名 去过的地方 去的时间
曹操 北京 1980
上海 1980
日本 1980
刘备 中山 2006
上海 2006
日本 2006
*/
[去过的地方],b.*
from
( select 姓名,去过的地方,单号,row_number() over (partition by 姓名 order by 去过的地方) rn from table_name)a
inner join tb2 b on a.单号 = b.单号
go
--> -->
if not object_id(N'Tempdb..#TB1') is null
drop table #TB1
Go
Create table #TB1([姓名] nvarchar(2),[去过的地方] nvarchar(2),[单号] int)
Insert #TB1
select N'曹操',N'北京',1 union all
select N'曹操',N'上海',1 union all
select N'刘备',N'中山',2 union all
select N'刘备',N'上海',2 union all
select N'刘备',N'日本',2 union all
select N'曹操',N'日本',1
Go
if not object_id(N'Tempdb..#TB2') is null
drop table #TB2
Go
Create table #TB2([单号] int,[去的时间] int)
Insert #TB2
select 1,'1980' union all
select 2,'2006'
Goselect case rn when 1 then [姓名] else '' end as 姓名,
[去过的地方],b.*
from
( select 姓名,去过的地方,单号,row_number() over (partition by 姓名 order by 去过的地方) rn from tb1)a
inner join tb2 b on a.单号 = b.单号
没有东西出来。
Insert TB1
select N'曹操',N'北京',1 union all
select N'曹操',N'上海',1 union all
select N'刘备',N'中山',2 union all
select N'刘备',N'上海',2 union all
select N'刘备',N'日本',2 union all
select N'曹操',N'日本',1Create table TB2([姓名] nvarchar(2),[去的时间] int)
Insert TB2
select N'曹操','1980' union all
select N'刘备','2006'select (case when 去过的地方=(select min(去过的地方)
from tb1 a where a.姓名=tb1.姓名)
then tb1.姓名 else '' end ) as 姓名,
tb1.去过的地方,tb2.去的时间
from tb1,tb2
where tb1.姓名=tb2.姓名/*
姓名 去过的地方 去的时间
---- ----- -----------
曹操 北京 1980
上海 1980
中山 2006
上海 2006
刘备 日本 2006
日本 1980
*/