Podczas kwerendy Cletus jest poprawna, to warto zwrócić uwagę, że można użyć UNPIVOT słów kluczowych w SQL Server 2005 i później zrobić prawie to samo:
select
period, nameOfVehicle, value
from T unpivot (
value for nameOfVehicle in ([Car],[Truck],[Boat])
) as U;
Różnica między UNPIVOT and Cletus za rozwiązaniem jest UNPIVOT nie będzie zawierać wierszy, dla których [value] IS NULL. Jeśli potrzebne są wartości null, musisz być nieco podstępny i użyć wartości, które nigdy nie może pojawić się w tabeli (tu używam pusty ciąg znaków):
with X(period,Car,Truck,Boat) as (
select period,coalesce(Car,''),coalesce(Truck,''),coalesce(Boat,'')
from T
)
select
period, nameOfVehicle, case when value = '' then null else value end as value
from X unpivot (
value for nameOfVehicle in ([Car],[Truck],[Boat])
) as U;
zależności od kolumn będziesz utrzymać po unpivoting, to może być innej opcji (to rozwiązanie zachowa wartości null):
select
period,
nameOfVehicle,
max(case nameOfVehicle
when 'Truck' then Truck
when 'Car' then Car
when 'Boat' then Boat end) as value
from T cross join (
values ('Truck'),('Car'),('Boat')
) as Columns(nameOfVehicle)
group by period, nameOfVehicle;
masz zamiar muszą być bardziej szczegółowe na temat danych, które mają - jak dokładnie powinno zawartość NameOfVehicle być ustalona, etc – Amber
Zawartość NameAndVehicle to Ciężarówka, Samochód i Łódź. – theringostarrs