in reply to Merging/Rearranging Tables

If you're on a *nix system and don't absolutely have to do this with perl, the join command would take care of this quite simply... Dump the tables to files (sorted on Field1, as join does require input files to be sorted), then something like join -a1 -a2 -t\| file1 file2 should do it. The main disadvantage of this method is that join only works on two files at a time.

To do it in perl, your first idea is on the right track, and is one of the fastest/most efficient ways to do this with a couple minor optimizations:

- Make a single list of all unique ID values from all tables. As already suggested, a hash would be ideal for this, since it will eliminate any duplicates.
- Sort this list and sort all tables by ID.
- Walk the list of unique IDs and grab the matching record (if any) from each table. Since everything's sorted, you can just run a single query against each table and only advance a record when a match is found.

This gives you a total of two queries against each table, one to collect all IDs at the start, then one to grab the data by ID after that list is built.

Replies are listed 'Best First'.
Re^2: Merging/Rearranging Tables
by homeveg (Acolyte) on Feb 11, 2007 at 01:21 UTC
    Unfortunately I can use only Windows-based PC. That's why I choose Perl.

    My prototype script is working more or less as you described, except I did not think before about making single Unique IDs list. First I thought about it, when I was writing my question here :)
    And I was not clever enough to organize final arrangement in one query only.

    Thanks a lot!