Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

requête SQL; horizontale à verticale

Vous pouvez utiliser un UNION ALL :

select No, 'Flag_1' as FlagName, Flag_1 as Flag_Value
from yourtable
union all
select No, 'Flag_2' as FlagName, Flag_2 as Flag_Value
from yourtable
union all
select No, 'Flag_3' as FlagName, Flag_3 as Flag_Value
from yourtable

Ou un UNPIVOT :

select no, FlagsName, flag_value
from yourtable
unpivot
(
    flag_value
    for FlagsName in (Flag_1, Flag_2, Flag_3)
) u

Voir SQL Fiddle avec démo