作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道您可以使用 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
最佳答案
另一种方法是在 unpivot 之前使用别名,如下所示:
;with aliasedUsers as (
select
UserId,
HomePhone as [3000]
OfficePhone as [3001]
CellPhone as [3002]
Fax as [3003]
Website as [3005]
)
select UserId
, ContactMethod
, ContactMethodValue
from aliasedUsers
unpivot (
ContactMethodValue for ContactMethod in
( [3000]
, [3001]
, [3002]
, [3003]
, [3005]
)
) as unpvt
关于sql-server-2008 - Sql 服务器 : How do I unpivot with an alias?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16464981/
我是一名优秀的程序员,十分优秀!