0

我正在尝试将行转换为列。这是我的查询

SELECT M.urn,
       M.eventdate,
       M.eventlocation,
       M.eventroom,
       M.eventbed,
       N.time
FROM   admpatevents M
       INNER JOIN admpattransferindex N
               ON M.urn = N.urn
                  AND M.eventseqno = N.eventseqno
                  AND M.eventdate = N.eventdate
WHERE  M.urn = 'F1002754364'
       AND M.eventcode = 'TFRADMIN'

当前结果

URN 日期 位置 房间 床 时间
F1002754364 20121101 EDEXPRESS 4-152 02 0724
F1002754364 20121101 CARDSURG 3-110 02 1455
F1002754364 20121102 胸部单元 6-129-GL04 1757

要求的结果

F1002754364 20121101 EDEXPRESS 4-152 02 0724 20121101 CARDSURG 3-110 02 1455 20121102 胸部单元 6-129-GL 04 1757

谢谢你的帮助。

4

1 回答 1

1

由于您使用的是 SQL Server,因此您可以同时使用UNPIVOTPIVOT函数来转换此数据。如果您知道您将拥有多少个值,那么您可以对这些值进行硬编码,如下所示:

select *
from
(
    select urn,
        value,
        col +'_'+ CAST(rn as varchar(10)) as col
    from 
    (
        SELECT M.urn,
            cast(M.eventdate as varchar(50)) eventdate,
            M.eventlocation,
            M.eventroom,
            M.eventbed,
            cast(N.time as varchar(50)) time,
            ROW_NUMBER() over(PARTITION by m.urn order by m.eventdate) rn
        FROM   admpatevents M
        INNER JOIN admpattransferindex N
            ON M.urn = N.urn
            AND M.eventseqno = N.eventseqno
            AND M.eventdate = N.eventdate
        WHERE  M.urn = 'F1002754364'
            AND M.eventcode = 'TFRADMIN'
    ) src1
    unpivot
    (
        value 
        for col in (eventdate, eventlocation, eventroom, eventbed, time)
    ) unpiv
) src2
pivot
(
    max(value)
    for col in ([eventdate_1], [eventlocation_1], [eventroom_1], [eventbed_1], [time_1],
                [eventdate_2], [eventlocation_2], [eventroom_2], [eventbed_2], [time_2],
                [eventdate_3], [eventlocation_3], [eventroom_3], [eventbed_3], [time_3])
) piv

注意 - 未经测试

如果您有未知数量的列,那么您可以使用类似于这样的动态 sql:

DECLARE  @query  AS NVARCHAR(MAX),
    @colsPivot as  NVARCHAR(MAX)

select @colsPivot = STUFF((SELECT  ',' 
                      + quotename(c.name +'_'+ cast(t.rn as varchar(10)))
                    from
                    (
                        select ROW_NUMBER() over(PARTITION by m.urn order by m.eventdate) rn
                        FROM   admpatevents M
                        INNER JOIN admpattransferindex N
                            ON M.urn = N.urn
                            AND M.eventseqno = N.eventseqno
                            AND M.eventdate = N.eventdate
                        WHERE  M.urn = 'F1002754364'

                    ) t
                    cross apply sys.columns as C
                   where C.object_id IN (object_id('admpatevents'), object_id('admpattransferindex')) and
                         C.name not in ('urn') -- add any other columns your want to exclude
                   group by c.name, t.rn
                   order by t.rn
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query 
  = 'select *
     from
     (
       select urn, value, col +''_''+ CAST(rn as varchar(10)) as col
       from
       (
            SELECT M.urn,
                cast(M.eventdate as varchar(20)) eventdate,
                M.eventlocation,
                M.eventroom,
                M.eventbed,
                cast(N.time as varchar(20)) time,
                ROW_NUMBER() over(PARTITION by m.urn order by m.eventdate) rn
            FROM   admpatevents M
            INNER JOIN admpattransferindex N
                ON M.urn = N.urn
                AND M.eventseqno = N.EVENTseqno
                AND M.eventdate = N.eventdate
            WHERE  M.urn = ''F1002754364''

       ) x
       unpivot
       (
         value
         for col in (eventdate, eventlocation, eventroom, eventbed, time)
       ) u
      ) x1
      pivot
      (
        max(value)
        for col in ('+ @colspivot +')
      ) p'

exec(@query)

请参阅带有演示的 SQL Fiddle

于 2012-12-04T17:10:05.583 回答