Results 1 to 5 of 5

Thread: [RESOLVED] selective rows between two tables

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Resolved [RESOLVED] selective rows between two tables

    Hi all

    i have a little problem and i want to see if i can address it from sql instead using CODE

    i have two tables, the first one is the base table, its structure is something like this:

    Code:
    id,col1,col2,col3
    values:
    Code:
     1, 'some text',int value(5),int value(112)
    the second table contains copies of some records from the first table
    structure:
    Code:
    id,col1,col2,col3,row_id
    values
    Code:
    10,'some other text',int value(5),int value(112),1
    the columns "row_id" in table 2 stores the pk id of table 1

    that's mean that table 2 contains a copy of the row with the id of 1 from table 1 but with different text value.

    what i want to achieve is that with a single select i'll get all rows from table 1 but if there is a copy of any row in table 2 it will replace the original row from table 1 in the select result.

    is there any way to do it with pure sql or should i filter them only after i get the result from the two tables ? (i'm trying to save the two select statements)

    Thanks and best regards!
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: selective rows between two tables

    I think this is what you want:
    Code:
    SELECT id, col1, col2, col3
    FROM table2
    
    UNION
    
    SELECT id,col1,col2,col3
    FROM table1
    WHERE id NOT IN (SELECT id FROM table2)

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: selective rows between two tables

    Code:
    Select T1.Id
        ,IsNull(T2.Col1,T1.Col1) "Col1"
        ,IsNull(T2.Col1,T1.Col2) "Col2"
        ,IsNull(T2.Col3,T1.Col3) "Col3"
        From Table1 T1
        Left Join Table2 T2 on T2.RowId=T1.Id
    This ONLY RETURNS rows for every Id in the PK of Table1.

    Table2 replaces the values ONLY IF a row exists in that table.

    If Table2 has a RowId of 50, lets say - and 50 no longer exists as a Id in Table1 - then 50 will not be included.

    Although I think there is a different JOIN method that would get around that...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: selective rows between two tables

    Thanks guys, I'm just about getting to the part where i need to start wrestling with this, i will get back with the results very soon.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: selective rows between two tables

    Hi, it's been a while but i only got to this now and i decided to go with the union option which work perfect in this case, thank you both for the help.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width