Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Mendapatkan Pivot-Table yang Dihasilkan Secara Dinamis menjadi Tabel Temp

Berlari ke masalah ini hari ini, dan diposting di blog saya. Penjelasan singkat solusinya, adalah membuat tabel sementara dengan satu kolom, lalu ALTER secara dinamis menggunakan sp_executesql. Kemudian Anda dapat memasukkan hasil PIVOT dinamis ke dalamnya. Contoh kerja di bawah ini.

CREATE TABLE #Manufacturers
(
    ManufacturerID INT PRIMARY KEY,
    Name VARCHAR(128)
)

INSERT INTO #Manufacturers (ManufacturerID, Name)
VALUES (1,'Dell')
INSERT INTO #Manufacturers (ManufacturerID, Name)
VALUES (2,'Lenovo')
INSERT INTO #Manufacturers (ManufacturerID, Name)
VALUES (3,'HP')

CREATE TABLE #Years
(YearID INT, Description VARCHAR(128))
GO

INSERT INTO #Years (YearID, Description) VALUES (1, '2014')
INSERT INTO #Years (YearID, Description) VALUES (2, '2015')
GO

CREATE TABLE #Sales
(ManufacturerID INT, YearID INT,Revenue MONEY)
GO

INSERT INTO #Sales (ManufacturerID, YearID, Revenue) VALUES(1,2,59000000000)
INSERT INTO #Sales (ManufacturerID, YearID, Revenue) VALUES(2,2,46000000000)
INSERT INTO #Sales (ManufacturerID, YearID, Revenue) VALUES(3,2,111500000000)
INSERT INTO #Sales (ManufacturerID, YearID, Revenue) VALUES(1,1,55000000000)
INSERT INTO #Sales (ManufacturerID, YearID, Revenue) VALUES(2,1,42000000000)
INSERT INTO #Sales (ManufacturerID, YearID, Revenue) VALUES(3,1,101500000000)
GO

DECLARE @SQL AS NVARCHAR(MAX)
DECLARE @PivotColumnName AS NVARCHAR(MAX)
DECLARE @TempTableColumnName AS NVARCHAR(MAX)
DECLARE @AlterTempTable AS NVARCHAR(MAX)

--get delimited column names for various SQL statements below
SELECT 
    -- column names for pivot
    @PivotColumnName= ISNULL(@PivotColumnName + N',',N'') + QUOTENAME(CONVERT(NVARCHAR(10),YearID)),
    -- column names for insert into temp table
    @TempTableColumnName = ISNULL(@TempTableColumnName + N',',N'') + QUOTENAME('Y' + CONVERT(NVARCHAR(10),YearID)),
    -- column names for alteration of temp table
    @AlterTempTable = ISNULL(@AlterTempTable + N',',N'') + QUOTENAME('Y' + CONVERT(NVARCHAR(10),YearID)) + ' MONEY' 
FROM (SELECT DISTINCT [YearID] FROM #Sales) AS Sales

CREATE TABLE #Pivot
(
     ManufacturerID INT
)

-- Thats it! Because the following step will flesh it out.

SET @SQL = 'ALTER TABLE #Pivot ADD ' + @AlterTempTable
EXEC sp_executesql @SQL

--execute the dynamic PIVOT query into the temp table 
SET @SQL =  N'
    INSERT INTO #Pivot (ManufacturerID, ' + @TempTableColumnName + ')
    SELECT ManufacturerID, ' + @PivotColumnName + '
    FROM #Sales S
    PIVOT(SUM(Revenue) 
      FOR S.YearID IN (' + @PivotColumnName + ')) AS PivotTable'
EXEC sp_executesql @SQL

SELECT M.Name, P.*
FROM #Manufacturers M
INNER JOIN #Pivot P ON M.ManufacturerID = P.ManufacturerID


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Memahami SQL Server Selalu Terenkripsi

  2. Tentukan ukuran baris untuk tabel

  3. Kesalahan CTE:Jenis tidak cocok antara jangkar dan bagian rekursif

  4. Prosesor AMD EPYC 7002 Series dan SQL Server

  5. Skrip Inventaris Koleksi SQL Server -2