Beispiel für Pivot-Tabelle Verwendung dafür:
BEGIN TRAN
CREATE TABLE dbo.PivotExample (Country NVARCHAR(50) NULL
,Year SMALLINT NOT NULL
,SalesAmount MONEY NULL
)
GO
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Australia', 2005, 1309047.1978)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Germany', 2006, 521230.8475)
INSERT dbo.PivotExample
(Country
,Year
,SalesAmount
)
VALUES (N'United States'
,2007
,2838512.3550
)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'France', 2008, 922179.0400)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Australia', 2007, 3033784.2131)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'France', 2005, 180571.6920)
INSERT dbo.PivotExample
(Country
,Year
,SalesAmount
)
VALUES (N'United Kingdom'
,2006
,591586.8540
)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Canada', 2006, 621602.3823)
INSERT dbo.PivotExample
(Country
,Year
,SalesAmount
)
VALUES (N'United Kingdom'
,2005
,291590.5194
)
INSERT dbo.PivotExample
(Country
,Year
,SalesAmount
)
VALUES (N'United States'
,2005
,1100549.4498
)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Canada', 2007, 535784.4624)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'France', 2007, 1026324.9692)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Germany', 2007, 1058405.7305)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Australia', 2006, 2154284.8835)
INSERT dbo.PivotExample
(Country
,Year
,SalesAmount
)
VALUES (N'United Kingdom'
,2008
,1210286.2700
)
INSERT dbo.PivotExample
(Country
,Year
,SalesAmount
)
VALUES (N'United States'
,2008
,3324031.1600
)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Germany', 2008, 1076890.7700)
INSERT dbo.PivotExample
(Country
,Year
,SalesAmount
)
VALUES (N'United Kingdom'
,2007
,1298248.5675
)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Australia', 2008, 2563884.2900)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Canada', 2005, 146829.8074)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Germany', 2005, 237784.9902)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'Canada', 2008, 673628.2100)
INSERT dbo.PivotExample
(Country
,Year
,SalesAmount
)
VALUES (N'United States'
,2006
,2126696.5460
)
INSERT dbo.PivotExample
(Country, Year, SalesAmount)
VALUES (N'France', 2006, 514942.0131)
GO
SELECT *
FROM dbo.PivotExample
ORDER BY Country
GO
SELECT Country
, [2005]
, [2006]
, [2007]
, [2008]
, [2009]
, [2010]
FROM dbo.PivotExample PIVOT
(SUM(SalesAmount) FOR Year IN ([2005], [2006], [2007], [2008], [2009], [2010])) P
--Declare necessary variables
DECLARE @SQLQuery AS NVARCHAR(MAX)
DECLARE @PivotColumns AS NVARCHAR(MAX)
--Get unique values of pivot column
SELECT @PivotColumns= COALESCE(@PivotColumns + ',','') + QUOTENAME(Year)
FROM (SELECT DISTINCT Year FROM [dbo].[PivotExample]) AS PivotExample
SELECT @PivotColumns
--Create the dynamic query with all the values for
--pivot column at runtime
SET @SQLQuery =
N'SELECT Country, ' + @PivotColumns + '
FROM [dbo].[PivotExample]
PIVOT(SUM(SalesAmount)
FOR Year IN (' + @PivotColumns + ')) AS P'
SELECT @SQLQuery
--Execute dynamic query
EXEC sp_executesql @SQLQuery
ROLLBACK TRAN
Credit http://www.databasejournal.com
vollständigen Link http://www.databasejournal.com/features/mssql/converting-rows-to-columns-pivot-and-columns-to-rows-unpivot-in-sql-server.html
eine Lese der vollständigen Seite haben, ist es ziemlich gut, und was ich auf Basis meiner Code an.
Wo zeigen Sie diese Daten an? Diese Art von Dingen wird normalerweise am besten in Ihrer Präsentationsebene behandelt. – iamdave
Viele Benutzer benutzen Excel, also müssen Sie die Tabelle für Excel - Pivots geeignet machen. – JustLukas