在SQL SERVER 2008中将静态列与动态列组合在一起

时间:2013-04-16 18:46:21

标签: sql sql-server sql-server-2008

我有一个表SchoolStudent,有三列和以下值

enter image description here

现在,对于学生,我们可以拥有动态列,这些列存储在表中,其值为:

enter image description here

我想要一个查询,我可以在单个SQL查询中添加静态字段和动态字段。我尝试使用Pivot但没有成功。请建议如何实现这一目标。我基本上这样做,所以在前端我有一个可以通过搜索的结果集。

所以查询应该是:

Select 
    StudentID, FirstName, Address, MotherName, 
    MotherEmail, MotherOccupation, Mother Salary, 
    FacebookProfileName, VehicleRegNo 
from SchoolStudent 
Inner Join 
    (Logic to convert rows to columns)

3 个答案:

答案 0 :(得分:2)

由于您有动态列,因此您需要使用动态SQL来生成所需的结果。此查询将生成第二个表中的列列表并将它们PIVOT,然后将其连接到SchoolStudent以获取其他列:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(DynamicColumnName) 
                    from StudentDetails
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = '
         select s.studentid,
            s.firstname,
            s.address,
            d.*
         from SchoolStudent s
         inner join 
         (
            SELECT studentId,' + @cols + ' 
            from 
            (
                select studentId, dynamiccolumnname, 
                  dynamiccolumnvalue
                from StudentDetails
            ) x
            pivot 
            (
                max(dynamiccolumnvalue)
                for dynamiccolumnname in (' + @cols + ')
            ) p 
          ) d
            on s.StudentId = d.StudentId'

execute(@query);

SQL Fiddle with Demo。此查询给出结果:

| STUDENTID | FIRSTNAME |   ADDRESS | FACEBOOK PROFILE NAME | MOTHER EMAIL | MOTHER NAME | MOTHER OCCUPATION | MOTHER SALARY | VEHICLE REG NO |
-----------------------------------------------------------------------------------------------------------------------------------------------
|         4 |     admin | efewfwfew |                  ross |  ram@ram.com | ram@ram.com |       ram@ram.com |   ram@ram.com |       UP 14 as |

答案 1 :(得分:1)

你可以使用这样的子查询格式:

SELECT t1.StudentID, 
       t1.FirstName, 
       t1.Address, 
       (SELECT t2.DynamicColumnValue FROM table2 t2 WHERE t2.StudentID = t1.StudentID AND t2.DynamicColumnName = 'Mother Name') AS 'MotherName',
       (SELECT t2.DynamicColumnValue FROM table2 t2 WHERE t2.StudentID = t1.StudentID AND t2.DynamicColumnName = 'Mother Email') AS 'MotherEmail'
FROM Table1 t1

或者您可以使用这样的内部联接格式:

SELECT t1.StudentID, 
       t1.FirstName, 
       t1.Address,
       t2.DynamicColumnValue AS 'MotherName'
       t3.DynamicColumnValue AS 'MotherEmail'
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.StudentID = t2.StudentID AND t2.DynamicColumnName = 'Mother Name'
INNER JOIN Table2 t3 ON t1.StudentID = t3.StudentID AND t2.DynamicColumnName = 'Mother Email'

答案 2 :(得分:1)

使用一些动态sql,你可以这样做:

DECLARE @cols NVARCHAR(2000)
SELECT  @cols = STUFF((SELECT DISTINCT '],[' + DynamicColumnName FROM DynamicValues ORDER BY '],[' + DynamicColumnName FOR XML PATH('')), 1, 2, '') + ']'

DECLARE @query NVARCHAR(4000);
SET @query = N'select s.StudentID, s.FirstName, s.Address,'+@cols+' from SchoolStudent s
join
(
    select * from DynamicValues
    pivot (Max(DynamicColumnValue) for DynamicColumnName in ('+@cols+')) as pvt
) as b on b.StudentID=s.StudentID'
EXECUTE(@query)