Query Question

  • I have a situation where I am joining multiple tables together (SQL Server 2005 - Standard Edition). Some of the tables have similar field names.

    I’d like to display those field names as different field names (change “description”, to “Offense_Description”, etc).

    I also want to dump this data in a temp table and keep those field name changes.

    Can this be done?

    Thank you very much for your response.

  • Yes, this can be done using an alias. Example:

    SELECT t1.col1 AS t1_col1

    ,t1.col2 AS t1_col2

    ,t2.col1 AS t2_col1

    ,t2.col2 AS t2_col2

    FROM table1 AS t1

    INNER JOIN table2 AS t2 ON t2.key = t1.key

    In the above query, I am aliasing table1 as t1, table2 as t2, table1 col1 as t1_col1, etc...

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • And, yes, it's easy to dump that into a temp table...

    Yes, this can be done using an alias. Example:

    SELECT t1.col1 AS t1_col1,

    t1.col2 AS t1_col2,

    t2.col1 AS t2_col1,

    t2.col2 AS t2_col2

    INTO #somenewtemptable

    FROM table1 AS t1

    INNER JOIN table2 AS t2 ON t2.key = t1.key

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

  • Oops - forgot the temp table, thanks Jeff.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Jeffrey Williams (8/16/2008)


    Oops - forgot the temp table, thanks Jeff.

    No problem... it's the beauty of a forum... world's biggest team. 😉

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

  • Thanks Jeff & Jeffrey!!!

  • You bet... 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)

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

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