Sql server 转换表中列中的行

Sql server 转换表中列中的行,sql-server,Sql Server,我正在尝试旋转一个表的可视化,该表将行显示为列,而不进行任何聚合 假设我有这张桌子 create table user id int, name nvarchar(100), company nvarchar(100), division nvarchar(100), city nvarchar(100) 可以通过此选择检索的 select name,company division, city from

我正在尝试旋转一个表的可视化,该表将行显示为列,而不进行任何聚合

假设我有这张桌子

create table user  
    id       int,  
    name     nvarchar(100),  
    company  nvarchar(100),  
    division nvarchar(100),  
    city     nvarchar(100)
可以通过此选择检索的

select name,company division, city from user order by id  
威奇给了我这个结果

john    Company1   division1   City1  
Peter   Company2   division2   City2  
Mary    Company3   division3   City3 
.
.
但我需要的是将每一行显示为一列,并在第一列中显示字段名称,如下所示

Name     john        Peter      Mary       ....  
Company  Company1    Company2   Company3   ....  
Division division1   division2  division3  ....  
City     City1       City2      City3      ....
我怎样才能做到这一点?我试着用这个unpivot

select col,value
from
    (select cast(name as varchar) as name,
            cast(Company as varchar) as company,
            cast(Division as varchar) as division
            cast(City as varchar) as city
        from user) p
    unpivot
    (value for col in (name,company,division,city)) as unpvt
但这是我得到的提示:我希望所有的名字都在同一行

name     john  
Company  Company1  
Division division1  
City     City1  
name     peter             // this should be in the first row as a second column  
Company  Company2          
Division division2  
City     City2
...

这是非常难看的,但这是我唯一能弄明白如何在SQL Server中实现您想要的功能的方法。如果复制并粘贴代码,它将运行并给出结果,同时保持数据库干净。我使用了两个永久表来解决一些动态sql范围限制,但在完成之前,我将它们都删除了

If      Object_ID('tempdb..#userInfo') Is Not Null Drop Table #userInfo
Create  Table #userInfo (id Int, name Varchar(100), company Varchar(100), division Varchar(100), city Varchar(100))

Insert  #userInfo (id, name, company, division, city)
Values  (1, 'john','company1', 'division1', 'city1'),
        (2, 'peter','company2', 'division2', 'city2'),
        (3, 'mary','company3', 'division3', 'city3'),
        (4, 'timmy','company4', 'division4', 'city4'),
        (5, 'nancy','company5', 'division5', 'city5'),
        (6, 'james','company6', 'division6', 'city6'),
        (7, 'brandon','company7', 'division7', 'city7'),
        (8, 'jay','company8', 'division8', 'city8')

If      Object_ID('tempdb..#unPivoted') Is Not Null Drop Table #unPivoted
Create  Table #unPivoted (id Int, rid Int, col Varchar(100), value Varchar(100))

Insert  #unPivoted
Select  id, Row_Number() Over (Partition By id Order By value) As rID, col, value
From    #userInfo p
Unpivot (value For col In (name, company, division, city)) As u

If      Object_ID('dbo.TempQueryOutput') Is Not Null Drop Table dbo.TempQueryOutput

Select  1 As OrderCol,'City' As ColName Into dbo.TempQueryOutput
Union
Select  2,'Company'
Union
Select  3,'Division'
Union
Select  4,'Name'


Declare @sql Nvarchar(Max),
        @maxID Int,
        @loopIter Int = 1

Select  @maxID = Max(id)
From    #userInfo

While   @loopIter <= @maxID
Begin
        Set     @sql = 'Select  o.*, u.value As Col' + Convert(Nvarchar(100),@loopIter) + ' Into dbo.TempQueryTable
                        From    dbo.TempQueryOutput o
                        Join    #unPivoted u
                                On  o.OrderCol = u.rid
                                And u.id = ' + Convert(Nvarchar(100),@loopIter)

        Exec    sp_executeSQL @sql

        If      Object_ID('dbo.TempQueryOutput') Is Not Null Drop Table dbo.TempQueryOutput

        Select  * Into dbo.TempQueryOutput
        From    dbo.TempQueryTable

        If      Object_ID('dbo.TempQueryTable') Is Not Null Drop Table dbo.TempQueryTable

        Set     @loopIter = @loopIter + 1

End

Update  dbo.TempQueryOutput
Set     OrderCol =  Case 
                    When ColName = 'Name' Then 1
                    When ColName = 'Company' Then 2
                    When ColName = 'Division' Then 3
                    When ColName = 'City' Then 4
                    End

Select  *
From    dbo.TempQueryOutput
Order   By OrderCol             

If      Object_ID('dbo.TempQueryOutput') Is Not Null Drop Table dbo.TempQueryOutput

如果你有很多用户名怎么办?您将有很长的列列表。很难管理。@ThitLwinOo我知道这个问题。目前最坏的情况是682列,但这是客户机的请求。我将在查询中实现过滤器,试图限制问题,但这是我所能做的一切。我建议您在应用程序级别更好地管理它。不在db级别。过滤器将是客户端在屏幕中有权访问的参数,他将选择在屏幕上生成文件创建,输出将导出到Excel谢谢。我认为没有比这更好的了。