SELECT MONTH_01 AS '01'
FROM ABC
您的回覆我測試可以 謝謝您
但是若是用結合UNPIVOT就不行, 請問哪裏有問題?
SELECT * FROM
(SELECT
MONTH_01 AS '01',
MONTH_02 AS '02',
FROM abc
) as p
UNPIVOT
(
MONTH_B for MON in (01,02)
)
as pv
(SELECT
MONTH_01 AS '01',
MONTH_02 AS '02'
FROM abc
) as p
SELECT dte,sort FROM
(
SELECT convert(varchar,getdate(),112) as dte,
100 AS 't01',200 AS 't02'
) as p
UNPIVOT
(
sort FOR Qty IN (t01,t02)
) as pv
我的需求是將圖一的資料轉成圖二,麻煩賜教, 謝謝
我只能這麼寫,請問有更簡潔的嗎?
SELECT year
,SUBSTRING (MON_12,2,2) AS MON
,COST
FROM
(SELECT year
,[M01] AS M01
,[M02] AS M02
,[M03] AS M03
,[M04] AS M04
,[M05] AS M05
,[M06] AS M06
,[M07] AS M07
,[M08] AS M08
,[M09] AS M09
,[M10] AS M10
,[M11] AS M11
,[M12] AS M12
FROM ABC
) as p
UNPIVOT
(
COST for MON_12 in (M01,M02,M03,M04,M05,M06,M07,M08,M09,M10,M11,M12)
)
as pv
我有一個小小的建議~
請問SQL 指令欄位名稱 AS 可以是數字或01變文字嗎?
SELECT MONTH_01 AS 01
FROM ABC
這個是題目。
SELECT MONTH_01 AS '01'
FROM ABC
這個是回答。
此樓大大已回答完成了。
如果有其他疑問,可以再提另外一個發問。一直無限延伸下去,其實大大們也不知道該怎麼回答到什麼程度才到頭。
以上報告完畢~
基本上,樞紐這一類的轉置我已經不太考慮使用MS SQL語法處理了,大部分都是用LinQ,考量到後續的維護,才是一整套系統最大的問題。
但基本上,樓上大大已經回復您的問題了,至少給個最佳解答是很簡單的事情吧。