で列にエイリアスを使用できることは知っていますが、でもエイリアスpivot
を使用したいと思いunpivot
ます。
select UserId
, ContactMethod
, ContactMethodValue
from Users
unpivot (
ContactMethodValue for ContactMethod in
( HomePhone as [3000]
, OfficePhone as [3001]
, CellPhone as [3002]
, Fax as [3003]
, Website as [3005]
)
) as unpvt
ただし、これを行うとエラーが発生します。
私が最終目標を達成することができた唯一の方法は、節でcase
ステートメントを使用することですが、これはきれいではありません。select
select UserId
, ( case ContactMethod
when 'HomePhone' then 3000
when 'OfficePhone' then 3001
when 'CellPhone' then 3002
when 'Fax' then 3003
when 'Website' then 3005
end ) as ContactMethod
, ContactMethodValue
from Users
unpivot (
ContactMethodValue for ContactMethod in
( HomePhone
, OfficePhone
, CellPhone
, Fax
, Website
)
) as unpvt
より良い方法はありますか?