SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with SQL Query


Help with SQL Query

Author
Message
Shadab Shah
Shadab Shah
SSC-Addicted
SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)

Group: General Forum Members
Points: 459 Visits: 798
Consider i have the below table structure


create table #temp(id int,entity nvarchar(50))
insert into #temp values(1,'Candy')
insert into #temp values(2,'Chocolate')

create table #temp2(name nvarchar(50),id int,value int)
insert into #temp2 values('Johnson',1,50)
insert into #temp2 values('Johnson',2,70)
insert into #temp2 values('Darick',1,30)
insert into #temp2 values('Darick',2,90)




I want the output as

name id candy chocolate
Johnson 1 50 70
Darick 1 30 90

I tried using pivot but could not acheive what i want. And coding @ afternoon is Doze
PravB4u
PravB4u
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 603
seems to be complex, but you can try this..
SELECT pv.NAME , min(ID) id, Candy , Chocolate from (
SELECT Name , Candy , Chocolate from
(SELECT
Name, [entity] , value
FROM #temp a INNER JOIN #temp2 b on a.id = b.id
) x
pivot
(
sum (value)
FOR [entity] IN (Candy , Chocolate)
) p
) as pv Left Join #temp2 a on a.name = pv.name
group by pv.NAME , Candy , Chocolate





Praveen D'sa
MCITP - Database Administrator 2008
http://sqlerrors.wordpress.com
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17186 Visits: 19557
Probably the best tool for this is a dynamic cross-tab. Jeff Moden has an excellent article here. If after reading the article you are still having difficulty, post back.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search