Sql server 2008 Sql Server:如何使用别名取消PIVOT?

Sql server 2008 Sql Server:如何使用别名取消PIVOT?,sql-server-2008,tsql,unpivot,Sql Server 2008,Tsql,Unpivot,我知道可以在具有pivot的列上使用别名,但我也希望在具有unpivot的列上使用别名 然而,当我这样做的时候,我得到了一个错误 我能够实现最终目标的唯一方法是在select子句中使用case语句,这不太好 select UserId , ( case ContactMethod when 'HomePhone' then 3000 when 'OfficePhone' then 3001 when 'CellPhone'

我知道可以在具有pivot的列上使用别名,但我也希望在具有unpivot的列上使用别名

然而,当我这样做的时候,我得到了一个错误

我能够实现最终目标的唯一方法是在select子句中使用case语句,这不太好

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函数中指定别名,因此必须使用CASE表达式

另一种方法是使用UNION ALL并立即放置新值:

select userid, 3000 as ContactMethod, homePhone as ContactMethodValue
from users 
union all
select userid, 3001 as ContactMethod, OfficePhone as ContactMethodValue
from users 
union all
select userid, 3002 as ContactMethod, CellPhone as ContactMethodValue
from users 
union all
select userid, 3003 as ContactMethod, Website as ContactMethodValue
from users 
union all
select userid, 3005 as ContactMethod, homePhone as ContactMethodValue
from users 

无法在UNPIVOT函数内指定别名,因此必须使用CASE表达式

另一种方法是使用UNION ALL并立即放置新值:

select userid, 3000 as ContactMethod, homePhone as ContactMethodValue
from users 
union all
select userid, 3001 as ContactMethod, OfficePhone as ContactMethodValue
from users 
union all
select userid, 3002 as ContactMethod, CellPhone as ContactMethodValue
from users 
union all
select userid, 3003 as ContactMethod, Website as ContactMethodValue
from users 
union all
select userid, 3005 as ContactMethod, homePhone as ContactMethodValue
from users 

另一种方法是在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

另一种方法是在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

案例陈述就是你如何做到这一点。你不能在UNPIVOT函数中使用别名。@bluefeet:该死,太快了。我很害怕。请注意Oracle支持as语法,所以这不是一个遥不可及的需求。CASE语句是如何做到这一点的。你不能在UNPIVOT函数中使用别名。@bluefeet:该死,太快了。我很担心。请注意,Oracle支持as语法,因此这不是一个遥不可及的需求。此解决方案的问题是它可能需要多个表扫描。当您开始添加更多列时,过滤等性能会下降。在非平凡的示例中,取消激励可能会执行得更好。此解决方案的问题在于它可能需要多个表扫描。当您开始添加更多列时,过滤等性能会下降。取消激励可能在非平凡的示例上表现得更好。此方法在获得所需结果的同时,保持了与接受答案相比的取消激励的效率。此方法在获得所需结果的同时,保持了与接受答案相比的取消激励的效率。