Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,

Please assist me with this issue.

I have a table that displays the data as is shown below:

Name ID Amount Date
Peter A $10 12/02/2013
Peter B $15 12/02/2013
Peter C $12 12/02/2013
Peter A $12 12/02/2013
Mary B $18 12/02/2013
Mary C $12 12/02/2013
James A $5 12/02/2013

What I want to get from the above is as shown below. So help me with the query please.

Name A B C
Peter $22 $15 $12
Mary $18 $12
James $5

Please assist me with the query for the above.

Thanks in advance.
Posted
Comments
ZurdoDev 4-Nov-13 17:05pm    
Describe in English how you want it.
ekipongi 4-Nov-13 17:12pm    
I want a column of distinct name, and the actual data in the ID which are A, B and C to be columns and the Amounts to be the data for the ID columns. For clearity, in my initial table as is shown above there are 4 columns, Name, ID (which contains A, B & C), Amount and the Date. I just want to display distinct names with 3 columns as A,B & C. the amount should be under associated ID as is shown in the 2nd table above.
joshrduncan2012 4-Nov-13 17:13pm    
Help you with what? What have you tried so far to accomplish this? Where are you stuck?
ekipongi 4-Nov-13 19:48pm    
For the benefit of others below is the answer. After a bit of reading on Pivot in ms sql got the answer that I wanted.

select *
from
(
select Name, ID, Amount
from Table1
) src
pivot
(
sum(Amount)
for ID in ([A], [B], [C])
) piv;
prabhatsp 27-Nov-13 5:02am    
I have the sample problem ,bt i dont know the static column name ,any suggestions pls............!

1 solution

Use PIVOT - UNPIVOT

Using PIVOT and UNPIVOT[^]

Pivoting data in SQL Server[^](Alternate ways)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900