sql >> データベース >  >> RDS >> Sqlserver

動的にPIVOT、2つのテーブルのJOINから返された結果

    何かが足りないかもしれませんが、データをPIVOTできるはずですが、row_number()を実装する必要があります。 列の生成に役立ちます。

    重要なのは、次のようなクエリを使用することです。

    SELECT  ONE.UserID,
      ONE.Episode,
      ONE.Value,
      TWO.Details,
      'Details'
        +cast(row_number() over(partition by one.userid, one.episode
                               order by two.details) as varchar(10)) seq
    FROM TABLE1 ONE 
    INNER JOIN TABLE2 Two
      ON ONE.UserID = TWO.UserID 
    AND ONE.Episode = TWO.Episode
    

    これにより、新しい列名に一意のシーケンスが作成され、PIVOTを適用できます:

    select userid, episode,
      value,
      details1,
      details2
    from
    (
      SELECT  ONE.UserID,
        ONE.Episode,
        ONE.Value,
        TWO.Details,
        'Details'
          +cast(row_number() over(partition by one.userid, one.episode
                                  order by two.details) as varchar(10)) seq
      FROM TABLE1 ONE 
      INNER JOIN TABLE2 Two
        ON ONE.UserID = TWO.UserID 
      AND ONE.Episode = TWO.Episode
    ) d
    pivot
    (
      max(details)
      for seq in (Details1, Details2)
    ) piv;
    

    SQL Fiddle withDemo を参照してください。 。次に、これを動的SQLに変換できます:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT ',' + QUOTENAME('Details'+cast(seq as varchar(10))) 
                        from 
                        (
                          select 
                            row_number() over(partition by one.userid, one.episode
                                                    order by two.details) seq
                            FROM TABLE1 ONE 
                            INNER JOIN TABLE2 Two
                              ON ONE.UserID = TWO.UserID 
                            AND ONE.Episode = TWO.Episode
                        ) d
                        group by seq
                        order by seq
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT userid, episode, value, ' + @cols + ' 
                from 
                (
                 SELECT  ONE.UserID,
                    ONE.Episode,
                    ONE.Value,
                    TWO.Details,
                    ''Details''
                      +cast(row_number() over(partition by one.userid, one.episode
                                              order by two.details) as varchar(10)) seq
                  FROM TABLE1 ONE 
                  INNER JOIN TABLE2 Two
                    ON ONE.UserID = TWO.UserID 
                  AND ONE.Episode = TWO.Episode
                ) x
                pivot 
                (
                    max(details)
                    for seq in (' + @cols + ')
                ) p '
    
    execute sp_executesql @query;
    

    SQL Fiddle withDemo を参照してください。 。結果を出す:

    | USERID | EPISODE |     VALUE |  DETAILS1 |  DETAILS2 |
    |--------|---------|-----------|-----------|-----------|
    |      1 |       1 | VALUE 1-1 | Details 1 | Details 2 |
    |      1 |       2 | VALUE 1-2 | Details 1 | Details 2 |
    



    1. CURRENT_TIMESTAMPの例– MySQL

    2. AWSRDSインスタンスのアップグレードダウンタイム

    3. 自己参照のmysqlテーブルから親子階層を取得します

    4. postgresのすべてのレコードをTitlecase、最初の文字は大文字に変換します