2

我有一个包含三列和以下值的表 SchoolStudent

在此处输入图像描述

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

在此处输入图像描述

我想要一个查询,我可以在单个 SQL 查询中包含静态字段和动态字段。我尝试使用 Pivot 但没有成功。请就如何实现这一点提出建议。我基本上这样做是为了在前端我有一个可以通过搜索的结果集。

所以查询应该是:

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

3 回答 3

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 |
于 2013-04-16T19:38:38.373 回答
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'
于 2013-04-16T19:07:09.630 回答
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)
于 2013-04-16T19:34:14.590 回答