SQL - 使用 PIVOT圖表資料使用語法


Code:

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:

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

留言

這個網誌中的熱門文章

java BigDecimal 加減乘除,四捨五入,四則運算及比較

MS sqlServer資料庫移轉至MySQL-->利用MySQL WorkBench

java 數字轉字串 字串轉數字