INPUT Member GEO_A_0 GEO_A_1 GEO_A_2 GEO_B_0 GEO_B_1 GEO_B_2 GEO_D_0 GEO_D_1 GEO_D_2
Jon 0 1 0 1 0 0 1 0 0
Scott 1 0 0 1 0 0 1 0 0
Suresh 0 0 1 1 0 0 1 0 1
OUTPUT GEO_A Jon 0 1 0
GEO_A Scott 1 0 0
GEO_A Suresh 0 0 1
GEO_B Jon 1 0 0
GEO_B Scott 1 0 0
GEO_B Suresh 1 0 0
GEO_D Jon 1 0 0
GEO_D Scott 1 0 0
GEO_D Suresh 1 0 1
NOTE: GEO_A, GEO_B, GEO_D may grow 30-40 columns everyday like GEO_ABD, so we need to dynamically convert GEO_* columns to rows
A generic approach uses
union all
to unpivot the columns to rows:If you want to order the resultset: