SQL JOIN multiple tables with PIVOT -
i'm trying inner join 3 tables pivot on third table. joining isn't issue pivot's pretty new me , combining them has turned disaster. i'm not sure how following:
table 1 id mnemonic gender 1 skyl f 2 hans m table 2 id lastname firstname 1 skywalker leia 2 solo han table 3 id query response 1 red 1 b green 1 c blue 1 d yellow 2 black 2 b white 2 c orange 2 d purple final table id mnemonic gender lastname firstname query_a query_b 1 skyl f skywalker leia red green 2 hans m solo han black white
one area know i'm having issues dont want 'responses' table 3, when 'query' column 'a' or 'b'. might end creating 2 new tables replace table 3, can rid of pivot , inner join 4 tables.
i followed along sql server join tables , pivot not work more 2 tables not queries needed pivot'ed.
you need pivot table 3 - pivot table 3 cte expression below , join result table 1 , 2
pivot_cte ( id, query_a, query_b ) -- define cte query. ( select * ( select id, query_a, b query_b table3 ) table3 pivot ( max(response) query in ([a], [b]) ) piv;) select * pivot_cte join table1 on table1.id=pivot_cte.id join table2 on table2.id=pivot_cte.id
Comments
Post a Comment