在pivot sql中附近的语法

时间:2017-01-13 14:20:21

标签: sql-server tsql

我正在尝试在此format dd/MMM/yyyy

中获取约会

所以我使用了以下查询

select REPLACE(CONVERT(NVARCHAR,GETDATE(), 106), ' ', '/')  

但是当我尝试将它应用于下面的查询时,我收到了这个错误信息。

  

错误:消息102,级别15,状态1,行13错误的语法在','附近。

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

select @cols = STUFF((SELECT ',' + QUOTENAME(Name) 
                      from  dbo.DailySales INNER JOIN dbo.Restaurants ON dbo.DailySales.RestaurantID = dbo.Restaurants.RestaurantID

                    group by Name
                    order by Name
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT  CONVERT(NVARCHAR, SalesDate, 106, ' ', '/') AS [Sales Date],' + @cols + ' from 
             (
  select SalesDate, Restaurants.Name, GrossSales
  from  dbo.DailySales INNER JOIN dbo.Restaurants ON dbo.DailySales.RestaurantID = dbo.Restaurants.RestaurantID
   WHERE MONTH(SalesDate) = 1 AND YEAR(SalesDate) = 2017 
            ) x
            pivot 
            (
                sum(GrossSales)
                for Name in (' + @cols + ')
            ) p '

execute(@query);

2 个答案:

答案 0 :(得分:2)

您缺少动态查询和REPLACE函数

上的单引号
DECLARE 
    @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

SELECT @cols = STUFF((SELECT ',' + QUOTENAME(Name) 
                      FROM dbo.DailySales INNER JOIN dbo.Restaurants ON dbo.DailySales.RestaurantID = dbo.Restaurants.RestaurantID
                    group by Name
                    order by Name
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

SET @query = 'SELECT 
    REPLACE(CONVERT(NVARCHAR, SalesDate, 106), '' '', ''/'') AS [Sales Date],
    ' + @cols + ' 
    FROM
    (
        SELECT
            SalesDate, 
            Restaurants.Name, 
            GrossSales
        FROM
            dbo.DailySales 
        INNER JOIN dbo.Restaurants ON dbo.DailySales.RestaurantID = dbo.Restaurants.RestaurantID
        WHERE 
            MONTH(SalesDate) = 1 AND YEAR(SalesDate) = 2017 
    ) x
    pivot 
    (
        SUM(GrossSales)
        FOR Name IN (' + @cols + ')
    ) p '

EXECUTE(@query);

答案 1 :(得分:0)

您忘了添加REPLACE

REPLACE(CONVERT(NVARCHAR, SalesDate, 106), '' '', ''/'')

应该是:

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

select @cols = STUFF((SELECT ',' + QUOTENAME(Name) 
                      from  dbo.DailySales INNER JOIN dbo.Restaurants ON dbo.DailySales.RestaurantID = dbo.Restaurants.RestaurantID

                    group by Name
                    order by Name
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT  REPLACE(CONVERT(NVARCHAR, SalesDate, 106), '' '', ''/'') AS [Sales Date],' + @cols + ' from 
             (
  select SalesDate, Restaurants.Name, GrossSales
  from  dbo.DailySales INNER JOIN dbo.Restaurants ON dbo.DailySales.RestaurantID = dbo.Restaurants.RestaurantID
   WHERE MONTH(SalesDate) = 1 AND YEAR(SalesDate) = 2017 
            ) x
            pivot 
            (
                sum(GrossSales)
                for Name in (' + @cols + ')
            ) p '

execute(@query);