import query with sql pivot to excel

0

I have a query in sql with pivot that returns the dates in columns, I want to make that query empty in an excel sheet to be able to do operations but for some reason excel it does not matter the data can guide me and more than anything know if possible

DECLARE @columns varchar(MAX)
DECLARE @sql nvarchar(max)

SET @columns = STUFF(
 (
 SELECT
   ',' + QUOTENAME(LTRIM(Fecha))
 FROM
   (SELECT DISTINCT Fecha
    FROM Ventas.dbo.Listado_año
   ) AS T
 ORDER BY
 Fecha
 FOR XML PATH('')
 ), 1, 1, '');

 SET @sql = N'
 SELECT
   * 
  FROM
  (  
  SELECT  grupo, Tarifa_id,Fecha
  FROM Ventas.dbo.Listado_año
  ) AS t
  PIVOT   
  (
  SUM(tarifa_id)
  FOR Fecha IN (' + @columns + N')
  ) AS P ;'

EXEC sp_executesql @sql;
    
asked by Gaba Yuc 08.06.2018 в 03:31
source

0 answers