Arranging the data??

  • Let's say I have a table containing these values:

    Id name title

    --------------------------

    1 Seba Developer

    1 Joe DBA

    1 Steve Developer

    What should I do to get the similar output to this:

    Id name title name title name title

    ---------------------------------------------------------------------

    1 Seba Developer Joe DBA Steve Developer

    I tried pivot function, but the problem is that it is very static, the number of persons per id can change, obviously names and titles will also vary from one row to the next. Any way to automate it through SQL?

  • I wouldn't be very pretty, but you could use the following to find your maximum number of names and use some dynamic SQL to create your output. Depending on the size of your resultset this could get very complicated and long running Very Very fast. you may also think about creating a temp table and writing it out to there before you try to output it. Keep in mind that whatever your do to automate this coming from the database you'll also need the front end to handle and that could be much more difficult than getting the SQL written.

    SELECT TOP 1 COUNT(*) AS MyMAX

    FROM test

    GROUP BY ID

    ORDER BY COUNT(*) DESC

    -Luke.

    To help us help you read this[/url]For better help with performance problems please read this[/url]

  • Just curious... why do you have the same ID for 3 different people?

    --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)

  • think about this id as a department id and not the identity for the user, so we can assume that 1 is a value from a lookup table, which could be represented as 'IT'.

  • Then, this should do it... not sure WHY you want to do this to perfectly good data, but it will do it...

    [font="Courier New"]--drop&nbsptable&nbspyourtable,&nbsp#Scratchpad

    --=====&nbspPresets

    &nbsp&nbsp&nbsp&nbspSET&nbspNOCOUNT&nbspON

    --=====&nbspCreate&nbspa&nbspsample&nbspdata&nbsptable.&nbspTHIS&nbspIS&nbspNOT&nbspPART&nbspOF&nbspTHE&nbspSOLUTION

    &nbspCREATE&nbspTABLE&nbspyourtable&nbsp(ID&nbspINT,&nbspName&nbspVARCHAR(10),Title&nbspVARCHAR(15))

    &nbspINSERT&nbspINTO&nbspyourtable&nbsp(ID,&nbspName,&nbspTitle)

    &nbspSELECT&nbsp'1','Seba','Developer'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'1','Joe','DBA'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'1','Steve','Developer'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'2','Deb','Developer'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'3','Dave','DBA'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'3','James','Developer'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'3','Venkat','DBA'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'3','Bob','Developer'

    --=====&nbspCopy&nbspthe&nbspdata&nbspinto&nbspa&nbsptemp&nbsptable&nbspwith&nbsproom&nbspfor&nbspan&nbspadditional&nbspcolumn

    &nbspSELECT&nbspID,&nbspName,&nbspTitle,&nbspCAST(0&nbspAS&nbspINT)&nbspAS&nbspSecondKey

    &nbsp&nbsp&nbspINTO&nbsp#Scratchpad

    &nbsp&nbsp&nbspFROM&nbspyourtable

    --=====&nbspThis&nbspindex&nbspis&nbspan&nbspabsolute&nbspmust&nbspto&nbspget&nbspthe&nbspgrouped&nbsprunning&nbspcount

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbspfor&nbspthe&nbspSecondKey&nbspcolumn&nbspto&nbspwork

    &nbspCREATE&nbspCLUSTERED&nbspINDEX&nbspComposite&nbspON&nbsp#ScratchPad&nbsp(ID,Title,Name)

    --=====&nbspDeclare&nbspsome&nbspobvious&nbspnamed&nbspvariables

    DECLARE&nbsp@PrevID&nbspINT

    &nbsp&nbsp&nbsp&nbspSET&nbsp@PrevID&nbsp=&nbsp0

    DECLARE&nbsp@SecondCount&nbspINT

    DECLARE&nbsp@SQL0&nbspVARCHAR(8000)

    DECLARE&nbsp@SQL1&nbspVARCHAR(8000)

    DECLARE&nbsp@SQL2&nbspVARCHAR(8000)

    --=====&nbspCreate&nbspthe&nbspgrouped&nbsprunning&nbspcount&nbspin&nbspthe&nbspSecondKey&nbspcolumn

    &nbspUPDATE&nbsp#ScratchPad

    &nbsp&nbsp&nbsp&nbspSET&nbsp@SecondCount&nbsp=&nbspSecondKey&nbsp=&nbspCASE&nbspWHEN&nbspID&nbsp=&nbsp@PrevID&nbspTHEN&nbsp@SecondCount+1&nbspELSE&nbsp1&nbspEND,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp@PrevID&nbsp=&nbspID

    &nbsp&nbsp&nbspFROM&nbsp#ScratchPad&nbspWITH&nbsp(INDEX(Composite),TABLOCKX)

    --=====&nbspCreate&nbspthe&nbspnecessary&nbspdynamic&nbspSQL

    &nbspSELECT&nbsp@SQL0&nbsp=&nbsp'SELECT&nbspID,'

    &nbspSELECT&nbsp@SQL1&nbsp=&nbspISNULL(@SQL1+',','')+CHAR(10)&nbsp

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp+&nbsp'MAX(CASE&nbspWHEN&nbspSecondKey&nbsp=&nbsp'&nbsp+&nbspCAST(d.SecondKey&nbspAS&nbspVARCHAR(10))&nbsp+&nbsp'&nbsp'

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp+&nbsp'THEN&nbspName&nbsp&nbspELSE&nbsp''''&nbspEND)&nbspAS&nbspName'+&nbspCAST(d.SecondKey&nbspAS&nbspVARCHAR(10))&nbsp+&nbsp','+CHAR(10)

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp+&nbsp'MAX(CASE&nbspWHEN&nbspSecondKey&nbsp=&nbsp'&nbsp+&nbspCAST(d.SecondKey&nbspAS&nbspVARCHAR(10))&nbsp+&nbsp'&nbsp'

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp+&nbsp'THEN&nbspTitle&nbspELSE&nbsp''''&nbspEND)&nbspAS&nbspTitle'&nbsp+&nbspCAST(d.SecondKey&nbspAS&nbspVARCHAR(10))

    &nbsp&nbsp&nbspFROM&nbsp(SELECT&nbspDISTINCT&nbspSecondKey&nbspFROM&nbsp#ScratchPad)&nbspd

    &nbsp&nbspORDER&nbspBY&nbspd.SecondKey

    &nbspSELECT&nbsp@SQL2&nbsp=&nbspCHAR(10)+'FROM&nbsp#ScratchPad&nbspGROUP&nbspBY&nbspID'

    --=====&nbspExecute&nbspthe&nbspdynamic&nbspSQL

    --PRINT&nbsp@SQL0+@SQL1+@SQL2

    &nbsp&nbsp&nbspEXEC&nbsp(@SQL0+@SQL1+@SQL2)[/font]

    --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)

  • Good Lord that's a lot of Non Breaking Spaces...

    To help us help you read this[/url]For better help with performance problems please read this[/url]

  • They don't show if you copy and paste into Query Analyzer...

    --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)

  • They do on my machine.

    Wait, do you mean Query Analyzer or Management Studio? They show in SSMS.

    I can definitely see that Steve and the guys need to work on the formatting issues with the new version of this site. 😀

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • Thanks, the sql was a lot of help!

  • ISNULL(@SQL1+',','')+CHAR(10)

    Just curious - what does this line of code do and how?

    Noticed that if this is not there we just get one row but with this we get all the values for SECONDKEY

  • Jason Selburg (10/16/2007)


    They do on my machine.

    Wait, do you mean Query Analyzer or Management Studio? They show in SSMS.

    I can definitely see that Steve and the guys need to work on the formatting issues with the new version of this site. 😀

    Sorry Jason... I'm still using 2k and Query Analyzer... you would probably need to replace the non-breaking spaces with regular spaces...

    ... and, I agree... they need to make it so we can do some decent formatting of code with regular spaces and maybe even tabs.

    --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)

  • My pleasure... thank you for the feedback...

    --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)

  • SQLMAIN (10/16/2007)


    ISNULL(@SQL1+',','')+CHAR(10)

    Just curious - what does this line of code do and how?

    Noticed that if this is not there we just get one row but with this we get all the values for SECONDKEY

    If @SQL1 is null, and it is for the first "row", it substitutes an empty string for the null so we can concatenate stuff to it including a linefeed character (CHAR(10)). If @SQL1 is not null, it adds a comma to the end of the current value of @SQL1 and then the linefeed... in both cases, the lines that follow that are concatenated to the current value of @SQL1... overall, it inserts the commas between rows without having an extra column at the end of the SELECT list that's being built. It's an old trick for building CSV columns.

    --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 13 posts - 1 through 12 (of 12 total)

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