将数据显示为列名

Showing data as a column name

我有三张桌子。我称他们为 Table A,Table B,Table C。 我有想要的视图。

Table A
Aid         RegNum  BID         Value
2CE7D0A7    2000000 D5981DFC    OFFCRO
9D3C13AA    2000000 C58566C5    YCH - from 
9DDB90C4    2000000 812E9E75    Y

Table B 通过 Table B 在 Table A

中的外键连接到 Table A
Table B

BID             Label                               ColumnName              Order
D5981DFC        Offered/Change Role                 StatusChangeCode        0
C58566C5        Offered/Role Change Comments        StatusChangeComments    1
812E9E75        Assessed                            StatusChangeAssessed    2

Table C 在 Table A 中也有外键。注册号Reg num 是 Table C

中的主键
Table C 

Name        Surname     RegNum
Etibar      Hasanov     2000000

如您所见,Table B

中的列名是数据
DesiredView
Name        Surname     RegNum      StatusChangeCode    StatusChangeComments    StatusChangeAssessed
Etibar      Hasanov     2000000     OFFCRO              YCH - from              Y

您可以使用 PIVOT table 来实现。尝试这样的事情,

Create Table TableA(Aid varchar(50), RegNum int, BID Varchar(20), Value varchar(50))
insert into TableA values
('2CE7D0A7', 2000000, 'D5981DFC', 'OFFCRO'),
('9D3C13AA', 2000000, 'C58566C5', 'YCH - from' ),
('9DDB90C4', 2000000, '812E9E75', 'Y')


create Table TableB(BID Varchar(20), Label Varchar(50), ColumnName
 Varchar(50), [Order] int)

insert into TableB values
('D5981DFC', 'Offered/Change Role', 'StatusChangeCode', 0),
('C58566C5', 'Offered/Role Change Comments', 'StatusChangeComments', 1),
('812E9E75', 'Assessed', 'StatusChangeAssessed',  2)

Create Table TableC (Name Varchar(20), Surname  Varchar(20), RegNum int)
insert into TableC values
('Etibar', 'Hasanov', 2000000)

SELECT *
FROM (
    SELECT 
      Name, SurName, C.RegNum, Value, B.ColumnName 
     FROM TableC C
     JOIN TableA A ON C.RegNum = A.RegNum
     JOIN TableB B on B.BID = A.BID
) AS Source
PIVOT
(
  MAX(Value)
    FOR [ColumnName] 
    in ( [StatusChangeCode], [StatusChangeComments],  [StatusChangeAssessed] )
)AS Pvot

Sql Fiddle Demo

如果您使用SQL server 2005,根据Microsoft Technet

When PIVOT and UNPIVOT are used against databases that are upgraded to SQL Server 2005 or later, the compatibility level of the database must be set to 90 or higher.

我首先选择了列名称。在这个 table 中它带来了 [StatusChangeAssessed],[StatusChangeCode],[StatusChangeComments] 。然后在下一个查询中我设置了@col(列名)。

declare @nregnumber  nvarchar(10)='2000000';



   DECLARE @cols AS NVARCHAR(MAX), @runningquery  as nvarchar(max);       
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(rn) 
                    from
                    (
                      select columnname rn from tableb where exists ( select * from tablea where tablea.bid=tableb.bid and regnum=@nregnumber) 
                    ) t
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'');


set @runningquery='

SELECT *
FROM (
    SELECT 
      Name, SurName, C.RegNum, Value, B.ColumnName 
     FROM TableC C
     JOIN TableA A ON C.RegNum = A.RegNum
     JOIN TableB B on B.BID = A.BID
) AS Source
PIVOT
(
  MAX(Value)
    FOR [ColumnName] 
    in ( '+@cols+')
)AS Pvot'
exec (@runningquery)

特别感谢 Selva。