将数据显示为列名称

时间:2015-03-17 05:12:30

标签: sql tsql stored-procedures sql-server-2005 pivot

我有三张桌子。我把它们称为表A,表B,表C. 而且我希望得到我想要的观点。

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

表B通过表A中的表B的外键与表A连接

Table B

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

表C也有表A中的外键。 Reg Num。 Reg num是表C中的主键

Table C 

Name        Surname     RegNum
Etibar      Hasanov     2000000

如您所见,列B的名称是表B中的数据

DesiredView
Name        Surname     RegNum      StatusChangeCode    StatusChangeComments    StatusChangeAssessed
Etibar      Hasanov     2000000     OFFCRO              YCH - from              Y

2 个答案:

答案 0 :(得分:1)

您可以使用PIVOT表来实现此目的。试试这样的事情,

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

  

当PIVOT和UNPIVOT用于升级到的数据库时   SQL Server 2005或更高版本,数据库的兼容级别必须   设置为90或更高。

答案 1 :(得分:1)

我首先选择了列名。在此表中,它带来 [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)

特别感谢塞尔瓦。