參考來源
--
此範例限:SQL 2005/SQL 2008
Code_1:
Select * From dbo.銷售業績 Order By 年份, 月份
SELECT
年份,
[01] as '一月', [02] as '二月', [03] as '三月', [04] as '四月',
[05] as '五月', [06] as '六月', [07] as '七月', [08] as '八月',
[09] as '九月', [10] as '十月', [11] as '十一月', [12] as '十二月'
FROM (
SELECT 年份, 月份, 銷售量
FROM dbo.銷售業績
GROUP BY 年份, 月份, 銷售量
) as GroupTable
PIVOT
(
Sum(銷售量)
FOR 月份 IN ([01], [02], [03], [04], [05], [06], [07], [08], [09], [10], [11], [12])
) AS PivotTable
Code_2:
Select * From dbo.[銷售業績By季] Order By 年份, 季, 月份
SELECT
年份,
[Q1] as '第一季', [Q2] as '第二季', [Q3] as '第三季', [Q4] as '第四季'
FROM (
SELECT 年份, 季, SUM(銷售量) as '季加總'
FROM dbo.[銷售業績By季]
GROUP BY 年份, 季
) as GroupTable
PIVOT
(
Sum(季加總)
FOR 季 IN ([Q1], [Q2], [Q3], [Q4])
) AS PivotTable
沒有留言:
張貼留言