SQL Server列作为查询结果中的行

时间:2014-08-21 11:54:33

标签: sql sql-server querying

如何获取查询某些列并表现为行的表?

来源表

ID | Name | Funct    | Phone1 | Phone2 | Phone3
1  | John | boss     | 112233 | 114455 | 117788
2  | Jane | manager  | NULL   | NULL   | 221111
3  | Tony | merchant | 441100 | 442222 | NULL

通缉结果

ID | Name | Funct    | Phone  | Ord
1  | John | boss     | 112233 | 1
1  | John | boss     | 114455 | 2
1  | John | boss     | 117788 | 3
2  | Jane | manager  | 221111 | 3
3  | Tony | merchant | 441100 | 1
3  | Tony | merchant | 442222 | 2

Ord是一列,其中是原始列的订单号(Phone1...Phone3

编辑:

好的,当电话号码在分隔列中时,UNION会没问题,但如果来源跟随(一列中的所有数字)会怎么样?:

ID | Name | Funct    | Phones
1  | John | boss     | 112233,114455,117788
2  | Jane | manager  | 221111
3  | Tony | merchant | 441100,442222

我理解,列Ord是无意义的(在这种情况下忽略它),但如何将数字拆分为分隔行?

3 个答案:

答案 0 :(得分:2)

最简单的方法是使用union all

select id, name, funct, phone1 as phone, 1 as ord
from source
where phone1 is not null
union all
select id, name, funct, phone2 as phone, 2 as ord
from source
where phone2 is not null
union all
select id, name, funct, phone3 as phone, 3 as ord
from source
where phone3 is not null;

你可以用cross apply作为:

来写这个
select so.*
from source s cross apply
     (select s.id, s.name, s.funct, s.phone1 as phone, 1 as ord union all
      select s.id, s.name, s.funct, s.phone2 as phone, 2 as ord union all
      select s.id, s.name, s.funct, s.phone3 as phone, 3 as ord
     ) so
where phone is not null;

还有使用unpivotcross join / case的方法。

答案 1 :(得分:1)

请参阅下面的答案,

Declare @table table
(ID int, Name varchar(100),Funct varchar(100),Phones varchar(400))

Insert into @table Values 
(1,'John','boss','112233,114455,117788'),
(2,'Jane','manager','221111' ),
(3,'Tony','merchant','441100,442222')

Select * from @table

结果:

enter image description here

代码:

Declare @tableDest table
([ID] int, [name] varchar(100),[Phones] varchar(400))

Declare @max_len int,
        @count int = 1

Set @max_len =  (Select max(Len(Phones) - len(Replace(Phones,',','')) + 1)
                From    @table)

While @count <= @max_len
begin
    Insert  into @tableDest
    Select  id,Name,
            SUBSTRING(Phones,1,charindex(',',Phones)-1)
    from    @table
    Where   charindex(',',Phones) > 0
    union   
    Select  id,Name,Phones
    from    @table
    Where   charindex(',',Phones) = 0

    Delete from @table
    Where   charindex(',',Phones) = 0

    Update  @table
    Set     Phones =  SUBSTRING(Phones,charindex(',',Phones)+1,len(Phones))
    Where   charindex(',',Phones) > 0

    Set     @count = @count + 1
End
------------------------------------------
Select  * 
from    @tableDest
Order By ID
------------------------------------------

最终结果:

enter image description here

答案 2 :(得分:0)

       SELECT CONCAT(
      'CREATE TABLE New_Table (',GROUP_CONCAT(
                            DISTINCT CONCAT(Name, ' VARCHAR(50)')
                            SEPARATOR ','),');')
      FROM
      Previous_Table
      INTO @sql;
      PREPARE stmt FROM @sql;
      EXECUTE stmt;

此查询根据另一个表的列值在表中生成一行。这可能会对你有所帮助