TSQL Puzzle

  • 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.

  • Sound like row concatenation

    Try this link[/url]



    Clear Sky SQL
    My Blog[/url]

  • 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/61537
  • 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

  • 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/61537
  • 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.

  • http://www.sqlservercentral.com/articles/cross+tab/65048/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply