关于两张表合并的问题SQL

sevensevery 2011-09-28 10:06:40


Create Table T1(
FName varchar(80)
)
Create Table T2(
FName varchar(80)
)

Insert into T1
Select 'A'
Insert into T1
Select 'A'
Insert into T1
Select 'A'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'


Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'B'
Insert into T2
Select 'B'
Insert into T2
Select 'B'


Select * from T1
Select * from T2

合并成这样结果
FName1 FName2
A A
A A
A A
A
A
A
A
B B
B B
B B
B
B
B
C C
........
...全文
36 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sevensevery 2011-09-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 fredrickhu 的回复:]
2000的在8楼
[/Quote]

谢谢, 结贴!
--小F-- 2011-09-28
  • 打赏
  • 举报
回复
2000的在8楼
--小F-- 2011-09-28
  • 打赏
  • 举报
回复
Create Table T1(
FName varchar(80)
)
Create Table T2(
FName varchar(80)
)

Insert into T1
Select 'A'
Insert into T1
Select 'A'
Insert into T1
Select 'A'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'


Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'B'
Insert into T2
Select 'B'
Insert into T2
Select 'B'

select id=identity(int,1,1),* into #t1 from t1
select id=identity(int,1,1),* into #t2 from t2

select
a.fname as fname1,b.fname as fname2
from
(
select fname,px=(select count(1) from #t1 where fname=t.fname and ID<T.ID) from #t1 t
)a
full join
(
select fname,px=(select count(1) from #t2 where fname=t.fname and ID<T.ID) from #t2 t
)b
on
a.FName =b.FName and a.px =b.px



drop table t1,t2,#t1,#t2

/*fname1 fname2
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
A A
A A
A A
NULL A
NULL A
NULL A
NULL A
NULL A
B B
B B
B B
B NULL
B NULL

(13 行受影响)
*/
--小F-- 2011-09-28
  • 打赏
  • 举报
回复
select id=identity(int,1,1),* into #t1 from t1
select id=identity(int,1,1),* into #t2 from t2

select
a.fname as fname1,b.fname as fname2
from
(
select fname,px=(select count(1) from #t1 where fname=t.fname and ID<T.ID) from #t1 t
)a
full join
(
select fname,px=(select count(1) from #t2 where fname=t.fname and ID<T.ID) from #t2 t
)b
on
a.FName =b.FName and a.px =b.px
sevensevery 2011-09-28
  • 打赏
  • 举报
回复
2005的函数, 啊;晕, 我执行不了!!!!!
-晴天 2011-09-28
  • 打赏
  • 举报
回复
楼主,可以结帖了.
-晴天 2011-09-28
  • 打赏
  • 举报
回复
Create Table T1(
FName varchar(80)
)
Create Table T2(
FName varchar(80)
)

Insert into T1
Select 'A'
Insert into T1
Select 'A'
Insert into T1
Select 'A'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'


Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'B'
Insert into T2
Select 'B'
Insert into T2
Select 'B'
select a.fname as fname1,b.fname as fname2 from(
select fname,ROW_NUMBER ()over(partition by fname order by (select 1))rn from T1
)a full join(
select fname,ROW_NUMBER ()over(partition by fname order by (select 1))rn from T2
)b on a.FName =b.FName and a.rn =b.rn
/*
fname1 fname2
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
A A
A A
A A
NULL A
NULL A
NULL A
NULL A
NULL A
B B
B B
B B
B NULL
B NULL

(13 行受影响)

go
drop table t1,t2
-晴天 2011-09-28
  • 打赏
  • 举报
回复
Create Table T1(
FName varchar(80)
)
Create Table T2(
FName varchar(80)
)

Insert into T1
Select 'A'
Insert into T1
Select 'A'
Insert into T1
Select 'A'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'
Insert into T1
Select 'B'


Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'A'
Insert into T2
Select 'B'
Insert into T2
Select 'B'
Insert into T2
Select 'B'
select a.fname,b.fname from(
select fname,ROW_NUMBER ()over(partition by fname order by (select 1))rn from T1
)a full join(
select fname,ROW_NUMBER ()over(partition by fname order by (select 1))rn from T2
)b on a.FName =b.FName and a.rn =b.rn
/*
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
A A
A A
A A
NULL A
NULL A
NULL A
NULL A
NULL A
B B
B B
B B
B NULL
B NULL

(13 行受影响)

*/
sevensevery 2011-09-28
  • 打赏
  • 举报
回复
行数, 以T1或T2表, 最大行数为准;
sevensevery 2011-09-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fredrickhu 的回复:]
怎么直接没看懂 是什么规则?
[/Quote]

就是两张表,

要写个SQL语句, 并成一张表, 两个字段;

表是按ABC排序的, 但是每张表, 数目不一样, 要保证B之前, 两列是一致的, 没有则为空;
--小F-- 2011-09-28
  • 打赏
  • 举报
回复
怎么直接没看懂 是什么规则?

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧