Simple Select Table output (Pivot ?)

  • Declare @test-2 Table (First Nvarchar(10), Last Nvarchar(10))

    Insert into @test-2 (First, Last) values ('Tom' ,'Jones')

    Insert into @test-2 (First, Last) values ('John' ,'Jones')

    Select First, Last From @test-2

    Giving me the results of

    FirstLast

    TomJones

    JohnJones

    I need these results

    FirstTomJohn

    LastJonesJones

    If a third insert is added

    Insert into @test-2 (First, Last) values ('Mike' ,'Jones')

    I would get

    FirstTomJohnMike

    LastJonesJonesJones

    Running SQL2005

  • Have you read Jeff Moden's articles on cross tabs and Dynamic cross tabs? Search them in this site.

    I don't remember if ROW_NUMBER is mentioned on the article, but this will help you to start.

    SELECT First,

    Last,

    ROW_NUMBER() OVER( ORDER BY (SELECT NULL)) AS rn

    FROM @test-2

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • the results of your select are

    FirstLastrn

    TomJones1

    JohnJones2

    these are not the results I am look for thank you

  • I told you that was a start.

    What you need is dynamic code to include all columns needed.

    Have you read the articles?

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 4 posts - 1 through 3 (of 3 total)

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