sql server - sql join one row per column -


i have 2 tables follows:

product groupsize     ------------------           1         10                       2         15                        groupsize  size1  size2  size3  size4    --------------------------------------   10       100    200   15       50     100    300 

and want table this:

product  size --------------    1      100    1      200    2      50    2      100    2      300 

how can in sql?

the results have come query:

select 1 product, size1 size table2 size1 not null union select 2 product, size2 size table2 size2 not null union select 3 product, size3 size table2 size3 not null; 

this ansi standard sql , should work in database.

edit:

given revised question, can use cross apply, easier union all:

select t1.product, s.size table1 t1 join      table2 t2      on t1.groupsize = t2.groupsize      cross apply      (values(t2.size1), (t2.size2), (t2.size3)) s(size) s.size not null; 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -