Sql在日期上进行透视

时间:2018-06-14 07:04:42

标签: sql sql-server tsql

尝试将以下逻辑转换为SQL查询时,我收到此错误

逻辑:

TRANSFORM First([SirName] & "  - " & [SecondName]) AS Name
SELECT qry_Date.RoomNumber
FROM Guest RIGHT JOIN qry_Date ON Guest.ID = qry_Date.GuestID
WHERE (((qry_Date.RoomNumber) Is Not Null))
GROUP BY qry_Date.RoomNumber
PIVOT qry_Date.Date;

以下是我到目前为止所做的事情

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

SELECT @cols = STUFF((SELECT distinct top 100 percent
                        ',' + QUOTENAME(convert(NVARCHAR(MAX),qry_Date.Date,103))
                FROM qry_Date
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)'),1,1,'');

SET @query =  'SELECT Name, ' + @cols + '
FROM 
    (SELECT [SirName] +  '' - '' +  [SecondName] AS Name
    ,qry_Date.RoomNumber
    ,qry_Date.Date
FROM Guest RIGHT JOIN qry_DateTemp ON Guest.ID = qry_Date.GuestID
WHERE qry_Date.RoomNumber Is Not Null) as t 
PIVOT
(count(Name) FOR [Date] IN( ' + @cols + ') 
 ) as p'

print @query   
execute(@query)

哪个结果错误

  

Msg 8114,Level 16,State 1,Line 9转换数据类型时出错   nvarchar到datetime2。 Msg 473,Level 16,State 1,Line 9 The   不正确的价值" 13/12/2014"在PIVOT操作员中提供。消息   207,等级16,状态1,行1无效的列名称'名称'。

我的print @query输出

SELECT Name, [01/12/2014],[02/12/2014],[03/12/2014],[04/12/2014],[05/12/2014],[06/12/2014],[07/12/2014],[08/12/2014],[09/12/2014],[10/12/2014],[11/12/2014],[12/12/2014],[13/12/2014],[14/12/2014],[15/12/2014],[16/12/2014],[17/12/2014],[18/12/2014],[19/12/2014],[20/12/2014],[21/12/2014],[22/12/2014],[23/12/2014],[24/12/2014],[25/12/2014],[26/12/2014],[27/12/2014],[28/12/2014],[29/12/2014],[30/12/2014],[31/12/2014]
FROM 
    (SELECT [SirName] +  '' - '' +  [SecondName]  AS Name
    ,qry_Date.RoomNumber
    ,qry_Date.Date
FROM Guest RIGHT JOIN qry_Date ON Guest.ID = qry_Date.GuestID
WHERE qry_Date.RoomNumber Is Not Null) as t 
PIVOT
(count(Name) FOR [Date] IN( [01/12/2014],[02/12/2014],[03/12/2014],[04/12/2014],[05/12/2014],[06/12/2014],[07/12/2014],[08/12/2014],[09/12/2014],[10/12/2014],[11/12/2014],[12/12/2014],[13/12/2014],[14/12/2014],[15/12/2014],[16/12/2014],[17/12/2014],[18/12/2014],[19/12/2014],[20/12/2014],[21/12/2014],[22/12/2014],[23/12/2014],[24/12/2014],[25/12/2014],[26/12/2014],[27/12/2014],[28/12/2014],[29/12/2014],[30/12/2014],[31/12/2014]) 
 ) as p

2 个答案:

答案 0 :(得分:0)

处理此错误的一种快速方法是以相同方式将所有数据转换为varchar(max)。所有日期将成为标题后,所以它应该是字符串。

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

SELECT @cols = STUFF((SELECT distinct top 100 percent
                        ',' + QUOTENAME(CAST(qry_Date.Date AS NVARCHAR(MAX)))
                FROM qry_Date
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)'),1,1,'');



SET @query =  'SELECT RoomNumber, ' + @cols + '
FROM 
    (SELECT [SirName] +  '' - '' +  [SecondName] AS Name
    ,qry_Date.RoomNumber
    ,CAST(qry_Date.Date AS NVARCHAR(MAX)) as [Date]
FROM Guest RIGHT JOIN qry_DateTemp ON Guest.ID = qry_Date.GuestID
WHERE qry_Date.RoomNumber Is Not Null) as t 
PIVOT
(count(Name) FOR [Date] IN( ' + @cols + ') 
 ) as p'

print @query   
execute(@query)

答案 1 :(得分:0)

@DhruvJoshi按照你提出的方式做到这一点,而且我犯了错误。当我得到我的结果时,我不得不将查询更改为

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

SELECT @cols = STUFF((SELECT distinct top 100 percent
                        ',' + QUOTENAME(CAST(qry_Date.Date AS NVARCHAR(MAX)))
                FROM qry_Date
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)'),1,1,'');



SET @query =  'SELECT RoomNumber, ' + @cols + '
FROM 
    (SELECT [SirName] +  '' - '' +  [SecondName] AS Names
    ,qry_Date.RoomNumber
    ,CAST(qry_Date.Date AS NVARCHAR(MAX)) as [Date]
FROM Guest RIGHT JOIN qry_Date ON Guest.ID = qry_Date.GuestID
WHERE qry_Date.RoomNumber Is Not Null) as t 
PIVOT
(MAX([Names]) FOR [Date] IN( ' + @cols + ') 
 ) as p'

print @query   
execute(@query)