need help with a sql query

  • i have 2 tables in the following way

    table 1

    col1 col2

    abc 123

    def 345

    ghi 675

    jkl 111

    table 2

    col3 col2

    455 123

    798 345

    098 675

    124 111

    and i would like a dynamic sql and a simple sql query to return the result set in the following way (re1 re2 re3 re4 are columns in a different table where i will be inserting the result set )

    re1 re2 re3 re4

    abc def ghi jkl

    455 798 098 124

    thanks

  • Look into the PIVOT command in Books Online. If you still have trouble re-post with questions on PIVOT. I think it will achieve what you are looking for.

    --------------------------------------------------------------------------
    When you realize you've dug yourself into a hole....Step 1...stop digging.

  • i did try in some ways ... but was having trouble achieving it

  • What are the datatypes of the columns?

    Looks like you are inserting varchars and numbers into the same column in your output.

    If you want people to help you, you need to help them first.

    You could do this by following the guidelines here

    http://qa.sqlservercentral.com/articles/Best+Practices/61537/

    Post what you tried with Pivot and what didn't work. Also, it is a better practice to give DDL and data scripts rather than a visual representation of how the table looks like.

  • sorry i forgot to mention the data types

    col1 is varchar

    col2 is int

    col3 is varchar

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

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