0

我正在尝试将列取消透视到 SQL Server 2008 中的行。

我想在行值中有空格和特殊字符(从列转置)。

一种方法是做一个 Replace like,EmpName As 'Employee_Name'然后在 Select 中做一个Replace(Title, '_', ' '). 但我有许多特殊字符需要处理。有什么办法可以解决这个问题吗?

下面的查询将不起作用,因为 select 语句中的选定值与我在 Unpivot 下使用的名称不同。

Select 
    Title, Value 
from 
    (Select Top 1 
         EmpName AS 'Employee Name', EmpSalary AS 'Employee Salary**', Test As 'Test: !'
     From 
         Emp 
     WHERE EmpNo ='1234') p
Unpivot
    (Value For Title In (EmpName, EmpSalary, Test)) As unpvt;
4

1 回答 1

1

尝试使用CROSS APPLY

SELECT Title, Value
FROM Emp
CROSS APPLY (
  VALUES
    ('Employee Name', EmpName),
    ('Employee Salary**', EmpSalary),
    ('Test: !', Test)
) upiv (Title, Value)
WHERE EmpNo ='1234'

或与UNPIVOT

SELECT Title, Value
FROM (
    SELECT [Employee Name] = EmpName, [Employee Salary**] = EmpSalary, [Test: !] = Test
    FROM Emp
    WHERE EmpNo ='1234'
) tbl
UNPIVOT(
    Value FOR 
    Title IN ([Employee Name], [Employee Salary**], [Test: !])
) upiv 
于 2013-11-11T16:18:50.903 回答