Entity Description Units AssetId
Vehicle 100 A Distance 8.48 2
Vehicle 100 A Fuel 11 2
Vehicle 100 A Parking 9 2
Vehicle 100 A Tolls 10 2
Vehicle 1 Distance 8.48 5
Vehicle 1 Fuel 8 5
Vehicle 1 Parking 6 5
Vehicle 1 Tolls 7 5
Transform above dataset to:
Vehicle Description Distance Fuel Parking Tolls AssetId
Vehicle 100 A 8.48 11 9 10 2
Vehicle 1 8.48 8 6 7 5
A simple conditional aggregation may do the trick.
Select [Vehicle Description] = Entity
,Distance = max(case when Description='Distance' then Units else null end)
,Fuel = max(case when Description='Fuel' then Units else null end)
,Fuel = max(case when Description='Parking' then Units else null end)
,Tolls = max(case when Description='Tolls' then Units else null end)
,AssetId = max(AssetID)
From YourTable
Group By Entity
Using the PIVOT syntax you can do it this way:
SELECT Entity [Vehicle Description],
Distance,
Fuel,
Parking,
Tolls,
AssetId
FROM
(
SELECT Entity,
Description,
Units,
Assetid
FROM yourtable) pt
PIVOT(SUM(units) FOR description
IN(Distance, Fuel, Parking, Tolls)) piv;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.