如何将列及其值移动到 MSSQL 中相同 table 的旋转列中

How to move a column and its value into pivoted column in same table in MSSQL

我有一个 table 的一些数据,格式如下

| LogDate  | AppName          | LogType | LogCount | GeneralError |
| 1-1-2017 | Registration App | Error   | 10       | 1            |
| 2-1-2017 | Reporting App    | Error   | 5        | 2            |

如何将 GeneralError 列移动到 LogType 并使用 t-sql 实现以下输出?

| LogDate  | AppName          | LogType       | LogCount |
| 1-1-2017 | Registration App | Error         | 10       |
| 2-1-2017 | Reporting App    | Error         | 5        |
| 1-1-2017 | Registration App | GeneralError  | 1        | 
| 2-1-2017 | Reporting App    | GeneralError  | 2        |

感谢您的帮助。

我会像这样做逆轴:

select t.LogDate, t.AppName, v.LogType, v.LogCount
from t cross apply
     (values ('Error', t.LogCount), ('GeneralError', t.GeneralError)
     ) v(LogType, LogCount);

从你陈述问题的方式来看,你也可以通过联合而不是枢轴结构来解决这个问题。

declare @t table (logdate date, appname varchar(50), logtype varchar(50), logcount int, generalerror int)
insert into @t values ('20170101','Registration App', 'Error', 10, 1),('20170102', 'Reporting App', 'Error', 5, 2)

select  * from (
select  logdate, appname, 'Error' as logtype, Logcount 
from    @t 
union 
select  logdate, appname, 'GeneralError' as logtype, GeneralError as Logcount 
from    @t 
) x order by logdate asc