SQL中动态PIVOT表中的错误

时间:2014-03-06 13:37:54

标签: sql sql-server pivot unpivot

我在动态PIVOT表中遇到错误。

我有一个类似的表 TT_Child

TTChild_ID      TT_ID       Roll_No     Std_Reg_ID      Attendance
1             3               1         22                     1
2             3               2         23                     0

和表

TT_Master

TT_ID   Attend_date  Time_from  Time_To   Course_ID  Faculty_ID  Acad_Year  Subject_ID
    1   2014-03-01  10:00   11:00       1         16     2013-2014     34
    2   2014-03-02  10:00   11:00       1         16     2013-2014        34
    3   2014-03-03  10:00   11:00       1         16     2013-2014    34

Student_Registration_Master

Std_Reg_ID  Stud_FNAME  stud_MNAME  Stud_MNAME

- 我正在使用此PIVOT查询,但收到此错误。我必须使用Attend_Date和Time作为列名。

Create PROCEDURE [dbo].[Attendance_Test]
    @courseid as int=null, @acadyear nvarchar(15)=null
AS

Declare @colList varchar(max)
Declare @qry varchar(max)

SET @colList = STUFF((SELECT distinct ',' + QUOTENAME(CONVERT(VARCHAR(19), TTM.Attend_Date, 103)) + ' '+ QUOTENAME(TTM.Time_From)
             FROM TT_Child SA
             inner join TT_Master TTM on SA.TT_ID = TTM.TT_ID
             where (TTM.Course_ID = @courseid) 
            FOR XML PATH(''), TYPE).value('/', 'NVARCHAR(MAX)') ,1,1,'')

SET @qry = 'SELECT SA.Reg_ID, STUD_FNAME +  STUD_MNAME + STUD_LNAME as [Student Name], '+@colList+' 
   FROM 
     (
        select SA.Reg_ID, SR.STUD_FNAME, SR.STUD_MNAME, SR.STUD_LNAME, TTM.Attend_Date , SA.Attendance from TT_Child SA
        inner join TT_Master TTM on SA.TT_ID = TTM.TT_ID
        inner join STUDENT_Registration_MASTER SR on CR.Reg_ID = SR.STUD_Reg_ID
        where (TTM.Course_ID = '+cast(@courseid as varchar(50))+ ') and (TTM.Acad_Year = '''+@acadyear+''')
        group by SA.Reg_ID, SR.STUD_FNAME, SR.STUD_MNAME, SR.STUD_LNAME, TTM.Attend_Date, SA.Attendance
     ) as s
    PIVOT
    (
        MAX(Attendance) FOR Attend_Date IN (' + @colList + ')
    ) pvt'
print(@qry)
Exec(@qry)

- 我收到此错误消息

SELECT SA.Reg_ID, STUD_FNAME +  STUD_MNAME + STUD_LNAME as [Student Name], [01/03/2014] [10:00:00.0000000],[02/03/2014] [10:00:00.0000000],[03/03/2014] [10:00:00.0000000],[05/03/2014] [10:00:00.0000000],[05/03/2014] [11:00:00.0000000] 
  FROM 
     (
       select SA.Reg_ID, SR.STUD_FNAME, SR.STUD_MNAME, SR.STUD_LNAME, TTM.Attend_Date , SA.Attendance from TT_Child SA
        inner join TT_Master TTM on SA.TT_ID = TTM.TT_ID
        inner join STUDENT_Registration_MASTER SR on CR.Reg_ID = SR.STUD_Reg_ID
        where (TTM.Course_ID = 1) and (TTM.Acad_Year = '2013-2014')
        group by SA.Reg_ID, SR.STUD_FNAME, SR.STUD_MNAME, SR.STUD_LNAME, TTM.Attend_Date, SA.Attendance
     ) as s
   PIVOT
    (
       MAX(Attendance) FOR Attend_Date IN ([01/03/2014] [10:00:00.0000000],[02/03/2014] [10:00:00.0000000],[03/03/2014] [10:00:00.0000000],[05/03/2014] [10:00:00.0000000],[05/03/2014] [11:00:00.0000000])
    ) pvt

错误消息

Msg 102, Level 15, State 1, Line 12
Incorrect syntax near '10:00:00.0000000'.

Plz给出解决方案

1 个答案:

答案 0 :(得分:1)

当您查看代码的这一行时,您会看到错误:

MAX(Attendance) 
FOR Attend_Date IN ([01/03/2014] [10:00:00.0000000],[02/03/2014] [10:00:00.0000000],
                    [03/03/2014] [10:00:00.0000000],[05/03/2014] [10:00:00.0000000],
                    [05/03/2014] [11:00:00.0000000])

日期和时间分别用方括号括起来,这样你就会收到错误。

您需要先将日期和时间连接在一起,然后使用QUOTENAME将其括在方括号中。您应该能够将代码更改为:

SET @colList 
  = STUFF((SELECT distinct ',' + QUOTENAME(CONVERT(VARCHAR(19), TTM.Attend_Date, 103) + ' '+ TTM.Time_From)
             FROM TT_Child SA
             inner join TT_Master TTM on SA.TT_ID = TTM.TT_ID
             where (TTM.Course_ID = @courseid) 
            FOR XML PATH(''), TYPE).value('/', 'NVARCHAR(MAX)') ,1,1,'')

请参阅您的版本的SQL Fiddle with Demo和此新版本。