Results 1 to 4 of 4

Thread: Help merging 2 tables

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Help merging 2 tables

    I have 2 tables Site_2002 and Site_2003. These represent the same data from different years. Instead of continuing to create new tables each year, I would like to merge these tables into a single table.

    What I’m looking to do is create a new table with a Fiscal Year column. Add the contents of Site_2002 and Site_2003 to the new table and at the same time set the values of the FY column to match whichever table the data is coming from.

    Could someone give me an idea on how to start this? I’m not very good with SQL and I’m not sure which functions are needed to pull this off. I hope with a nudge in the right direction I can figure this out. Any pseudo code would be greatly appreciated.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If your using Access:
    Add your new Fiscal_Year field to the first table and use it as the
    base. Populate the new field with 2002. Then rename Site_2002
    to something like Site_Data. Then select the Site_2003 table and
    copy it and paste it to the Site_Data table name and select
    Append data to existing table option. Then populate the
    remaining empty Fiscal_Year fields with 2003.

    If your using SQL server then:
    Use the INSERT INTO statement.
    Create the new filed Fiscal_Year in 2002. Populate it with 2002.
    Then create a default on that field of "YEAR(GetDate())".
    Then execute a statement like ...
    INSERT INTO
    Site_2003 (Col1, Col2, Col3...)
    VALUES
    (SELECT * FROM Site_2003)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Thanks for the suggestion. This is a SQl db. What I was looking into was adding a Fiscal_Year to both of the tables and updating it with the correct year. Then I was going to try to use a Union between the 2 table to create the final table. Does that seem like it may work?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Yes, that sound ok. Are you going to keep every year in
    separate tables? A union may end up being too resource
    intensive once you need to query the union for reports, etc.
    May be better to have the one table with all records in it then
    just have to execute one query on the table. Not union the two
    tables and then execute the query on it. Just something to keep in mind.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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