SQL 2005 ve Pivot Tablo

PIVOT deyimi kullanarak aşağıdaki şekilde satırlarınızı sütun haline getirebilirsiniz.

SELECT
[1]
AS ‘January’
,[2] AS ‘February’
,[3] AS ‘March’
,[4] AS ‘April’
,[5] AS ‘May’
,[6] AS ‘June’
,[7] AS ‘July’
,[8] AS ‘August’
,[9] AS ‘September’
,[10] AS ‘October’
,[11] AS ‘November’
,[12] AS ‘December’

FROM (

SELECT

MONTH(OrderDate) AS ‘MonthNumber’
,COUNT(*) AS ‘OrderCount’

FROM Orders WHERE YEAR(OrderDate) = 1997

GROUP BY MONTH(OrderDate)

) AS Data

PIVOT(

SUM(OrderCount)

FOR MonthNumber IN([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])

) AS PVT

Source: rss

KategorilerSQL