sql >> Database teknologi >  >> RDS >> Sqlserver

PIVOT dynamisk, returnerede resultater fra JOIN af to tabeller

Måske mangler jeg noget, men du burde være i stand til at PIVOT dataene, men du bliver nødt til at implementere row_number() for at hjælpe med at generere kolonnerne.

Nøglen vil være at bruge en forespørgsel, der ligner:

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
 

Dette vil skabe en unik sekvens for de nye kolonnenavne, så kan du anvende 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;
 

Se SQL Fiddle with Demo . Så kan du konvertere dette til dynamisk 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;
 

Se SQL Fiddle with Demo . Giver dig resultatet:

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


  1. Ordinal Not Found, når du kører Diesel CLI-værktøjet

  2. Oracle 12c PLSQL SOAP EXTRACTVALUE fra Array-svar

  3. PHP-kommandoer ude af synkronisering

  4. MySQL - sum kolonneværdi(er) baseret på række fra samme tabel