TSQL:将最后3列转换为行?

TSQL:将最后3列转换为行?,tsql,transpose,Tsql,Transpose,一直在尝试各种转置方法,包括pivot& 聚合/案例,但似乎没有任何东西正常工作。 希望转置(5列矩阵的)最后3列 进入下面每个唯一ID.图像的行中 这似乎是一个如此简单的问题。真的应该有 一个简单的解决方案 谁能给我指出正确的方向吗 有很多可能的解决方案,但我个人会使用CTE。要使CTE正常工作,您需要包含一个在开始处关闭以前的任何语句 我假设一个完全外部连接,以防任何EntrantID参与一个事件而不是另一个事件,如果不是这样,请根据您认为合适的方式更改连接 ; with log_thro

一直在尝试各种转置方法,包括pivot& 聚合/案例,但似乎没有任何东西正常工作。
希望转置(5列矩阵的)最后3列 进入下面每个唯一ID.图像的行中

这似乎是一个如此简单的问题。真的应该有 一个简单的解决方案

谁能给我指出正确的方向吗


有很多可能的解决方案,但我个人会使用CTE。要使CTE正常工作,您需要包含一个
在开始处关闭以前的任何语句

我假设一个
完全外部连接
,以防任何
EntrantID
参与一个事件而不是另一个事件,如果不是这样,请根据您认为合适的方式更改
连接

; with log_throw as
    (
        select t.*
        from #StrongmanTempTable as t
        where t.[Event] = 'Log Throw'
    )
    , tire_flip as
    (
        select t.*
        from #StrongmanTempTable as t
        where t.[Event] = 'Tire Flip'
    )
    , vehicle_pull as
    (
        select t.*
        from #StrongmanTempTable as t
        where t.[Event] = 'Vehicle Pull'
    )
select l.EntrantID
, l.Entrant
, l.[Event]
, l.Judge1Score
, l.Judge2Score
, f.[Event]
, f.Judge1Score
, f.Judge2Score
, p.[Event]
, p.Judge1Score
, p.Judge2Score
from log_throw as l
full outer join tire_flip as f on l.EntrantID = f.EntrantID
full outer join vehicle_pull as p on l.EntrantID = p.EntrantID
order by 1

谢谢你,塔希尔,它很管用!不幸的是,这只是现实世界问题的一个简单例子。我真正的问题是未知的“事件”,所以我无法将它们硬编码到解决方案中。有没有办法考虑到这一点?您必须使用动态SQL。我的另一个答案是最好的开始。
; with log_throw as
    (
        select t.*
        from #StrongmanTempTable as t
        where t.[Event] = 'Log Throw'
    )
    , tire_flip as
    (
        select t.*
        from #StrongmanTempTable as t
        where t.[Event] = 'Tire Flip'
    )
    , vehicle_pull as
    (
        select t.*
        from #StrongmanTempTable as t
        where t.[Event] = 'Vehicle Pull'
    )
select l.EntrantID
, l.Entrant
, l.[Event]
, l.Judge1Score
, l.Judge2Score
, f.[Event]
, f.Judge1Score
, f.Judge2Score
, p.[Event]
, p.Judge1Score
, p.Judge2Score
from log_throw as l
full outer join tire_flip as f on l.EntrantID = f.EntrantID
full outer join vehicle_pull as p on l.EntrantID = p.EntrantID
order by 1