July 9, 2009 at 1:15 pm
I have a report I need to create. However I'm stuck on an impossible puzzle.
I'm trying to add a column value from one row to another.
I've tried doing a self join however that created duplicate rows.
For example lets say column 1 from row 1 has the value 'sql' and column 1 from row 2 has the value 'server'.
I want to create a select statement that displays it as 'sql server'
However with my methods I end up with a row that shows 'sql server' and another that shows 'server sql'.
Please help 😉
Look at attachment for example.
For each person, I want the ballotcommitteename to combine with another ballotcommitteename value from another row with matching contactidname and asmemberof values.
July 9, 2009 at 1:23 pm
July 9, 2009 at 1:24 pm
Try changing
a.ballotcommitteename b. ballotcommitteename
to
a.ballotcommitteename < b. ballotcommitteename
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537July 9, 2009 at 2:02 pm
Thanks Guys.
Hey Mark I changed what you told me and it worked so far. However the problem is I don't know how many time's I'll have to add the column values to the roll.
Some users can have only one value, some two values, some three, and so on.
Damn this is hard
July 9, 2009 at 2:18 pm
Something like this then
WITH CTE AS (
SELECT pa_contactidname,asmemberof,ballotcommitteename,
ROW_NUMBER() OVER(PARTITION BY pa_contactidname,asmemberof ORDER BY ballotcommitteename) AS rn
FROM user_table)
SELECT pa_contactidname,asmemberof,
MAX(CASE WHEN rn=1 THEN ballotcommitteename END) AS ballotcommitteename1,
MAX(CASE WHEN rn=2 THEN ballotcommitteename END) AS ballotcommitteename2,
MAX(CASE WHEN rn=3 THEN ballotcommitteename END) AS ballotcommitteename3
FROM CTE
GROUP BY pa_contactidname,asmemberof
If you don't know the maximum number of ballotcommitteename in advance, you'll have
to construct the query dynamically
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537July 9, 2009 at 2:38 pm
You are the man. It worked however I do need to do it dynamically because I don't know the maximum number of ballotcommitteename in advance.
CTE is way new to me.
July 19, 2009 at 9:57 pm
http://www.sqlservercentral.com/articles/cross+tab/65048/
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply