Click here to Skip to main content
15,910,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table with following data..

A	B	C	D
BR00002	281	2.22	12-01-2013
BR00002	277	1.65	12-02-2013
BR00002	267	1.8	12-03-2013


I Want to get the following result..

X	y	12-01-2013	12-02-2013	12-03-2013
BR00002	825	2.22	         1.65	          1.8


Please help, How to get the required..
Posted
Comments
[no name] 24-Apr-14 8:24am    
You already know the answer, write a pivot query.

Learn to do it yourself:
Simple Way To Use Pivot In SQL Query[^]
++++++++++[round 2]++++++++++
As per request, try this:
SQL
SELECT  A as  X, Y, [12-01-2013],[12-02-2013],[12-03-2013]
FROM
(SELECT A,
    (SELECT SUM(B) from table1 t2 where t2.a=t1.a group by t2.a) as Y,
 C, D  FROM table1 t1) AS src
PIVOT
(
  SUM(C)  FOR D IN ([12-01-2013],[12-02-2013],[12-03-2013])
) as  pvt
 
Share this answer
 
v2
Comments
Member 10715787 24-Apr-14 9:37am    
Hi I am able to find the below result using pivot..
X 12-01-2013 12-02-2013 12-03-2013
BR00002 2.22 1.65 1.8

But i could not be able to Sum(Y) in the query.
I am using the following query to find the above result-
Select * FROM
(
SELECT A as X,SUM(C)as N,D group by X,D
)as s
PIVOT
(
SUM(C)
FOR D IN([12-01-2013],[12-02-2013],[12-03-2013])
) as pvt

Please help me to get the SUM of column(B) in same SP..
Please help..
Peter Leow 24-Apr-14 11:09am    
added in solution 1.
It is solved now.
I have taken a temp table to insert SUM(B) with group by A and join the both statement..
It has solved my problem..

Thanks for you guys to support and teach me about PIVOT. :)
 
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