Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reordering table data

Status
Not open for further replies.

Davmaster

Programmer
Mar 2, 2006
1
CA
I have a table that has 3 columns: ID, Type, and Value.

I'm looking for a way to rearrange/view the data in a matrix form where ID is the first index and Type is the second index, and Value is the value for that ID/Type.

i.e., I have:
ID Type Value
--------------
ID1 Type1 x
ID2 Type1 y
ID1 Type2 z
ID2 Type2 v

and I want to see it as:

Type1 Type2
--------------------
ID1 | x z
ID2 | y v

How would I write a query to do this? Or is it even possible?

Thanks much!
 
Check your RDBMS manual for CrossTable query or Pivot table.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
assuming value is numeric

select id,
sum(case when type = 'type1' then value else 0) as type1,
sum(case when type = 'type2' then value else 0) as type2
group by id
order by id

if it is not numeric replace sum with max, matter in this zero with the empty value equivelant for that data type.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top