日期:2014-05-18  浏览次数:20446 次

去掉每行的空数据列
有一人这样的表
 A B C 
  123 125
  45
145 41  
147  
654 75
要去掉每行的空列变成如下

 A B C 
 145 123 125
 147 41 45
 654 75


要怎么写sql呢
   
 

------解决方案--------------------
try:
select A,B,C from(
select row_number()over(order by getdate()) as id,A from tbl)a
full join(
select row_number()over(order by getdate()) as id,B from tbl)b
on a.id=b.id
full join(
select row_number()over(order by getdate()) as id,C from tbl)c
on a.id=c.id
------解决方案--------------------
CREATE TABLE [tabl](
[A] [int] NULL,
[B] [int] NULL,
[C] [int] NULL

GO
INSERT INTO [tabl] ([A],[B],[C]) VALUES (null ,123,125);
INSERT INTO [tabl] ([A],[B],[C]) VALUES (null ,null,45);
INSERT INTO [tabl] ([A],[B],[C]) VALUES (145 ,41,null);
INSERT INTO [tabl] ([A],[B],[C]) VALUES (147 ,null,null);
INSERT INTO [tabl] ([A],[B],[C]) VALUES (654 ,75,null);
GO
select * from [tabl] 
GO
select A,B,C from(
select row_number()over(order by getdate()) as id,A from [tabl] where A is not null)a
full join(
select row_number()over(order by getdate()) as id,B from [tabl] where B is not null)b
on a.id=b.id
full join(
select row_number()over(order by getdate()) as id,C from [tabl] where C is not null)c
on a.id=c.id