带有平均值列的SQL Pivot表

时间:2019-02-18 20:31:50

标签: sql-server pivot

我正在尝试使用Pivot将每个区域的平均价值放在列中,我不知道如何为我的数据创建它如何解决?这里的问题是,当我尝试运行下面的代码时,出现此错误

  

第15层状态1线14消息102
  'sq'附近的语法不正确。

我不知道该怎么做才能解决这个问题。

DECLARE @MovingAverageQty AS INT;
DECLARE @SkipQtyDays AS INT;
SET @MovingAverageQty = 4; 
SET @SkipQtyDays = @MovingAverageQty + 22; 

 DROP TABLE #solveAvg;

 CREATE TABLE #solveAvg
 (
      Id INT IDENTITY(1, 1), 
      Date DATE, 
      FDate DATE, 
      Avg1 DECIMAL(13, 8), 
      Avg2 DECIMAL(13, 8), 
      DataSource VARCHAR(100), 
      Region VARCHAR(100)
 );

 DECLARE @SQLStr VARCHAR(5000);

 SET @SQLStr = '';

 SELECT 
     @SQLStr = @SQLStr + '[' + [a].[Column] + '], '
 FROM
     (SELECT DISTINCT 
          Region AS [Column]
      FROM PriceQuote) AS a;

 SET @SQLStr = LEFT(@SQLStr, LEN(@SQLStr) - 1);

 INSERT INTO #solveAvg (Date, FDate, Avg1, Avg2, DataSource, Region)
     SELECT 
         Date AS d, 
         NULL, 
         AVG(Price) OVER (ORDER BY Date ASC ROWS 4 PRECEDING) AS AvgPrice, 
         0, 
         DataSource, 
         Region
     FROM 
         PriceQuote
     WHERE 
         ProductId = 15
         AND DataSource NOT IN ('MaisBolsas', 'QueroBolsa');

 UPDATE a
 SET a.Avg2 = b.Avg1, 
     FDate = b.Date
 FROM #solveAvg a
 INNER JOIN #solveAvg b ON a.Id = b.Id - @SkipQtyDays;

 SET @SQLStr = 'Select s.FDate as Date, Case when s.Avg1 = 0 then 0 else ((s.Avg2/s.Avg1) - 1)*100 end as series, i.Value as ponta,' + @SQLStr + ' from (#solveAvg s inner join ipca i on s.FDate = i.Date and i.ProductId = 15) sq PIVOT (series for Region in (' + @SQLStr + ')) as pt';

 PRINT @SQLStr;
 EXEC (@SQLStr);

编辑2:这是@SqlStr打印:

Select s.FDate as Date, i.Value as ponta,[ILHEUS], [TEIXEIRA DE FREITAS], [JAGUAQUARA], [CRUZ DAS ALMAS], [FEIRA DE SANTANA], [EUNAPOLIS], [VITORIA DA CONQUISTA], [RIBEIRA DO POMBAL], [IRECE], [TUCANO], [ITABUNA], [LUIS EDUARDO MAGALHAES], [VALENTE], [SANTO ANTONIO DE JESUS], [LIVRAMENTO DE NOSSA SENHORA], [JUAZEIRO], [CHICAGO/USA], [ITAPETINGA], [SALVADOR], [VALENCA], [BARREIRAS], [ADUSTINA], [INHAMBUPE], [JEQUIE] from (#solveAvg s inner join ipca i on s.FDate = i.Date and i.ProductId = 15) sq PIVOT (Case when s.Avg1 = 0 then 0 else ((s.Avg2/s.Avg1) - 1)*100 end for Region in ([ILHEUS], [TEIXEIRA DE FREITAS], [JAGUAQUARA], [CRUZ DAS ALMAS], [FEIRA DE SANTANA], [EUNAPOLIS], [VITORIA DA CONQUISTA], [RIBEIRA DO POMBAL], [IRECE], [TUCANO], [ITABUNA], [LUIS EDUARDO MAGALHAES], [VALENTE], [SANTO ANTONIO DE JESUS], [LIVRAMENTO DE NOSSA SENHORA], [JUAZEIRO], [CHICAGO/USA], [ITAPETINGA], [SALVADOR], [VALENCA], [BARREIRAS], [ADUSTINA], [INHAMBUPE], [JEQUIE])) as pt

1 个答案:

答案 0 :(得分:0)

告诉我们您在打印中获得的@SQLStr会有什么帮助,但是在构建动态SQL时,最后一个SET @SQLStr =会出现错误

Select s.FDate as Date, Case when s.Avg1 = 0 then 0 else ((s.Avg2/s.Avg1) - 1)*100 end as series, 
i.Value as ponta,' + @SQLStr + ' 
FROM (
    ----- NO FIELDS HERE??? ----
    #solveAvg s 
    INNER join ipca i on s.FDate = i.Date 
    AND i.ProductId = 15
) sq PIVOT (series for Region in (' + @SQLStr + ')) as pt';

您没有在嵌套的FROM(...)语句中选择任何内容