hi i have a table with columns as
Cost Rate
Repair 12
Repair 223
Wear 1000
Wear 666
Fuel 500
Repair 600
Fuel 450
Wear 400
Now i want this data as
Repair Wear Fuel
825 2066 950
Using Sql Query
Thanks in advance
You can use
GROUP BY
to combine results when using an aggregate function likeSUM()
.This will return the results inverted from what you requested, but that shouldn't be a big deal I hope.
This is an easy query using the
PIVOT
function. If you have a few values that are known to be converted to columns, then you can use a static version:If you have an unknown number of values, then you can use dynamic SQL to create a dynamic pivot:
See SQL Fiddle with Demo. Both will produce the same results.
The "rotation" you want (making rows into columns) can be obtained with SQL Server's PIVOT operator.
I think that you're looking for a way to do Dynamic Pivoting or Dynamic Cross-Tabs.
Check these articles:
You can do this with a simple Crosstab query. There is a wizard in the Access Queries window that will walk you through creating one. Just click the New Query button and select the Crosstab Query Wizard.