SELECT
totalvolume = SUM(volume),
totalusage = SUM(usage),
percentage = CAST(SUM(usage) * 100/SUM(volume) as decimal (10,2)),
YEAR
FROM
table1
查詢和我的表像下面
|totalvolume|totalusage|percentage|Year
|--------------------------------------
| 100 | 50 | 50.00 |2016
| 200 | 50 | 25.00 |2015
我的目標是使名單表是這樣這種使用PIVOT
:
|Type |2015 |2016 |
|------------------------ |
| totalvolume| 200 | 100 |
| totalusage | 50 | 50 |
| percentage |25.00|50.00 |
所以我決定先創建該表使用)UNPIVOT
:
Type | Value | year
-------------------
totalvolume |100 |2016
totalusage |50 |2016
percentage |50.00 |2016
totalvolume |200 |2015
totalusage |50 |2015
percentage |25.00 |2015
但在我UNPIVOT
查詢
SELECT
Type, Value, Year
FROM
<my table query>
UNPIVOT
(value FOR Type IN (totalvolume, totalusage, percentage))
有一個在我的SQL錯誤:
的類型與其他列中指定的類型列「百分比」衝突UNPIVOT list
那麼有什麼解決方案可以解決解決這個問題而不改變數據類型?
如果我排除百分比列,因此它是這樣
|Type |2015 |2016 |
|------------------------ |
| totalvolume| 200 | 100 |
| totalusage | 50 | 50 |
但我需要的百分比排,我也能拿我想用樞軸視圖。
在此先感謝
旋轉和格式(即「百分比」)都是報告工具函數,而不是數據庫函數。你對這些數據做了什麼?將它作爲報告發送?使用報告工具。 –