c# – 如何将行中的数据组合到列中

我有这样的结构.试图创建动态表

create table indicator(id int not null,name varchar(255) not null)
insert indicator (id, name) values (1, 'basic employee details')

create table fields(
id int identity(1,1) not null,  
type int,
name varchar(255) not null,
parentid int not null)

insert fields (id, type, name, parentid) values (1, 1, 'year', 0)
insert fields (id, type, name, parentid) values (2, 2, '2010', 1)
insert fields (id, type, name, parentid) values (5, 2, '2011', 1)
insert fields (id, type, name, parentid) values (6, 2, '2012', 1)
insert fields (id, type, name, parentid) values (7, 2, '2013', 1)
insert fields (id, type, name, parentid) values (8, 2, '2014', 1)
insert fields (id, type, name, parentid) values (9, 1, 'nationality', 0)
insert fields (id, type, name, parentid) values (10, 1, 'bahrani', 9)
insert fields (id, type, name, parentid) values (11, 1, 'non bahrani', 9)
insert fields (id, type, name, parentid) values (12, 1, 'gender', 0)
insert fields (id, type, name, parentid) values (13, 1, 'male', 12)
insert fields (id, type, name, parentid) values (14, 1, 'fe male', 12)
insert fields (id, type, name, parentid) values (15, 1, 'maritalstatus', 0)
insert fields (id, type, name, parentid) values (16, 1, 'married', 15)
insert fields (id, type, name, parentid) values (17, 1, 'unmarried', 15)
insert fields (id, type, name, parentid) values (18, 1, 'divorced', 15)

create table datafields(
    dataid int not null,
    fieldid int not null)

insert datafields (dataid, fieldid) values (1, 2)
insert datafields (dataid, fieldid) values (1, 10)
insert datafields (dataid, fieldid) values (1, 13)
insert datafields (dataid, fieldid) values (1, 16)
insert datafields (dataid, fieldid) values (2, 5)
insert datafields (dataid, fieldid) values (2, 11)
insert datafields (dataid, fieldid) values (2, 14)
insert datafields (dataid, fieldid) values (2, 17)


create table indicatorfields(
indicatorid int not null,
fieldid int not null)

insert indicatorfields (indicatorid, fieldid) values (1, 2)
insert indicatorfields (indicatorid, fieldid) values (1, 5)
insert indicatorfields (indicatorid, fieldid) values (1, 6)
insert indicatorfields (indicatorid, fieldid) values (1, 7)
insert indicatorfields (indicatorid, fieldid) values (1, 8)
insert indicatorfields (indicatorid, fieldid) values (1, 10)
insert indicatorfields (indicatorid, fieldid) values (1, 11)
insert indicatorfields (indicatorid, fieldid) values (1, 13)
insert indicatorfields (indicatorid, fieldid) values (1, 14)
insert indicatorfields (indicatorid, fieldid) values (1, 16)
insert indicatorfields (indicatorid, fieldid) values (1, 17)
insert indicatorfields (indicatorid, fieldid) values (1, 18)

create table indicatordata(
dataid int not null,
value varchar(255) not null,
indicatorid int not null) 

insert indicatordata (dataid, value, indicatorid) values (1, '1015', 1)
insert indicatordata (dataid, value, indicatorid) values (2, '12', 1)

我得到了这样的输出

《c# – 如何将行中的数据组合到列中》

我试图获得类似这样的数据

《c# – 如何将行中的数据组合到列中》

因为我正在尝试枢轴概念……

引导我得到类似这样的生成表

我试过这个,没有得到预期的输出

DECLARE @query NVARCHAR(4000),@cols NVARCHAR(4000),@groupid varchar(50)

set @groupid='1'
SELECT  @cols = COALESCE(@cols + ',[' + name + ']','[' + name + ']')
FROM    Fields where parentid=0
--ORDER BY name

SET @query = N'SELECT id, '+@cols +'
FROM
(
select d.dataid as id,b.name,e.value FROm IndicatorFields a INNER JOIN fields b On a.fieldid=b.id INNER JOIN fields c ON c.id=b.parentid
INNER JOIN DataFields d  ON a.fieldid=d.fieldid INNER JOIN IndicatorData e ON e.id=d.dataid
and a.indicatorid='+@groupid+') p
PIVOT
(
MAX([value])
FOR name IN
( '+@cols +' )
) AS pvt
ORDER BY id;'

print @query
EXECUTE(@query)

最佳答案 请不要将查询发布为图像.假设我正确地复制了您的查询,我认为这可以为您提供使用PIVOT所需的内容

然后你可以将它包装在一些动态的sql中 – 这里有大量的例子 – 使用搜索.

SELECT dataid
,MAX(value) Value
,MAX([Year]) Year
,MAX([Nationality]) Nationality
,MAX([Gender]) Gender
,MAX([MaritalStatus]) MaritalStatus
 FROM 
(SELECT 
      d.dataid
      ,b.name Name1
      ,d.fieldid
      ,c.name Name2
      ,e.value
FROM dbo.indicatorfields a
JOIN dbo.fields b ON a.fieldid = b.id
JOIN dbo.fields c ON b.parentid = c.id
JOIN dbo.datafields d ON a.fieldid = d.fieldid
JOIN dbo.indicatordata e ON e.dataid = d.dataid
)SRC
PIVOT 
(MAX(Name1) FOR Name2 IN ([Year],[Nationality],[Gender], [MaritalStatus]) )PVT
GROUP BY dataid

dataid  Value  Year  Nationality  Gender   MaritalStatus
------  ------ ----- ------------ ------   -------------
1       1015  2010   bahrani      male     married
2       12    2011   non bahrani  fe male  unmarried

Warning: Null value is eliminated by an aggregate or other SET operation.

(2 row(s) affected)

编辑:

基于您尝试使用上述PIVOT的动态SQL

CREATE table indicator(id int not null,name varchar(255) not null)
insert indicator (id, name) values (1, 'basic employee details')

create table fields(
id int  not null,  
type int,
name varchar(255) not null,
parentid int not null)

insert fields (id, type, name, parentid) values (1, 1, 'year', 0)
insert fields (id, type, name, parentid) values (2, 2, '2010', 1)
insert fields (id, type, name, parentid) values (5, 2, '2011', 1)
insert fields (id, type, name, parentid) values (6, 2, '2012', 1)
insert fields (id, type, name, parentid) values (7, 2, '2013', 1)
insert fields (id, type, name, parentid) values (8, 2, '2014', 1)
insert fields (id, type, name, parentid) values (9, 1, 'nationality', 0)
insert fields (id, type, name, parentid) values (10, 1, 'bahrani', 9)
insert fields (id, type, name, parentid) values (11, 1, 'non bahrani', 9)
insert fields (id, type, name, parentid) values (12, 1, 'gender', 0)
insert fields (id, type, name, parentid) values (13, 1, 'male', 12)
insert fields (id, type, name, parentid) values (14, 1, 'fe male', 12)
insert fields (id, type, name, parentid) values (15, 1, 'maritalstatus', 0)
insert fields (id, type, name, parentid) values (16, 1, 'married', 15)
insert fields (id, type, name, parentid) values (17, 1, 'unmarried', 15)
insert fields (id, type, name, parentid) values (18, 1, 'divorced', 15)

create table datafields(
    dataid int not null,
    fieldid int not null)

insert datafields (dataid, fieldid) values (1, 2)
insert datafields (dataid, fieldid) values (1, 10)
insert datafields (dataid, fieldid) values (1, 13)
insert datafields (dataid, fieldid) values (1, 16)
insert datafields (dataid, fieldid) values (2, 5)
insert datafields (dataid, fieldid) values (2, 11)
insert datafields (dataid, fieldid) values (2, 14)
insert datafields (dataid, fieldid) values (2, 17)


create table indicatorfields(
indicatorid int not null,
fieldid int not null)

insert indicatorfields (indicatorid, fieldid) values (1, 2)
insert indicatorfields (indicatorid, fieldid) values (1, 5)
insert indicatorfields (indicatorid, fieldid) values (1, 6)
insert indicatorfields (indicatorid, fieldid) values (1, 7)
insert indicatorfields (indicatorid, fieldid) values (1, 8)
insert indicatorfields (indicatorid, fieldid) values (1, 10)
insert indicatorfields (indicatorid, fieldid) values (1, 11)
insert indicatorfields (indicatorid, fieldid) values (1, 13)
insert indicatorfields (indicatorid, fieldid) values (1, 14)
insert indicatorfields (indicatorid, fieldid) values (1, 16)
insert indicatorfields (indicatorid, fieldid) values (1, 17)
insert indicatorfields (indicatorid, fieldid) values (1, 18)

create table indicatordata(
dataid int not null,
value varchar(255) not null,
indicatorid int not null) 

insert indicatordata (dataid, value, indicatorid) values (1, '1015', 1)
insert indicatordata (dataid, value, indicatorid) values (2, '12', 1)



DECLARE @query NVARCHAR(4000),@cols NVARCHAR(4000),@groupid varchar(50)

set @groupid='1'
SELECT  @cols = COALESCE(@cols + ',[' + name + ']','[' + name + ']')
FROM    Fields where parentid=0


DECLARE @sel NVARCHAR(4000);
SELECT  @sel = COALESCE(@sel + ',MAX([' + name + ']) '+name,',MAX([' + name + ']) '+name) FROM  Fields where parentid=0

SET @query = N'SELECT value, dataid' + @Sel +'
     FROM 
    (SELECT 
          d.dataid
          ,b.name Name1
          ,d.fieldid
          ,c.name Name2
          ,e.value
    FROM dbo.indicatorfields a
    JOIN dbo.fields b ON a.fieldid = b.id
    JOIN dbo.fields c ON b.parentid = c.id
    JOIN dbo.datafields d ON a.fieldid = d.fieldid
    JOIN dbo.indicatordata e ON e.dataid = d.dataid
    )SRC
    PIVOT 
    (MAX(Name1) FOR Name2 IN ('+@cols+') )PVT
    GROUP BY dataid, value'

print @query
EXECUTE(@query)
点赞