Combining columns from 3 tables and removing duplicates by date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maydie
    New Member
    • Aug 2010
    • 1

    #1

    Combining columns from 3 tables and removing duplicates by date

    I have 3 tables with the following relevant fields:

    ItemsFrmManufac turer
    Date......Seria lNum.....Descri ption.....Statu s
    01012010..00000 1..... Item1..... IN
    01022010..00000 2..... Item1..... IN

    ItemsShipped
    Date......Seria lNum.....Descri ption.....Statu s
    06012010..00000 1..... Item1..... OUT
    06012010..00000 3..... Item2..... OUT
    07152010..00000 1..... Item1..... OUT

    ItemsFromCustom erRepair
    Date......Seria lNum.....Descri ption.....Statu s
    05012010..00004 ..... Item3..... IN
    06302010..00001 ..... Item1..... IN

    I need to make a query or table takes the data from teh three tables and combines them into one table with the most recent record for each serial number.

    CurrentStatus
    Date......Seria lNum.....Descri ption.....Statu s
    07152010..00000 1..... Item1..... OUT
    01022010..00000 2..... Item1..... IN
    06012010..00000 3..... Item2..... OUT
    05012010..00000 4..... Item3..... IN

    Any help with a sql statement to do this would be greatly appreciated.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    Hi. Welcome to Bytes!

    I'm sorry to say that I think your data structure is all wrong (See Normalisation and Table structures). You should have a single table with an extra field which indicates which situation they reflect. That way, none of this is a problem. The data is already there just as you need it.

    This is not simply about this problem either. I can guarantee you that you will encounter further troublesome issues if you continue with your current design. Don't say you haven't been warned.

    That all said. Your design is fundamentally your concern, and the specific answer to your question would be :
    Code:
    SELECT 'Manufacturer' AS Source
         , *
    FROM   [ItemsFrmManufacturer]
    
    UNION ALL
    SELECT 'Shipped' AS Source
         , *
    FROM   [ItemsShipped]
    
    UNION ALL
    SELECT 'Repair' AS Source
         , *
    FROM   [ItemsFromCustomerRepair]

    Comment

    Working...