Re: Combining data from Temp Tables - Mailing list pgsql-general
From | David Johnston |
---|---|
Subject | Re: Combining data from Temp Tables |
Date | |
Msg-id | 00f101ccf0e6$209bd690$61d383b0$@yahoo.com Whole thread Raw |
In response to | Re: Combining data from Temp Tables (Jeff Herman <hermanj@hvpa.com>) |
List | pgsql-general |
>> Please follow the conventions of previous posters when adding a reply to an existing posting. If you are the first reply I would personally give more lee-way but this particular community prefers bottom-posting. -----Original Message----- From: David Johnston [mailto:polobo@yahoo.com] Sent: Tuesday, February 21, 2012 2:04 PM To: 'Andy Colson'; Jeff Herman Cc: pgsql-general@postgresql.org Subject: RE: [GENERAL] Combining data from Temp Tables -----Original Message----- From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of Andy Colson Sent: Tuesday, February 21, 2012 1:37 PM To: Jeff Herman Cc: pgsql-general@postgresql.org Subject: Re: [GENERAL] Combining data from Temp Tables how about select date, ln, mbrid, ds, ( select sum(ds) from t2 where t2.date >= t1.date and t2.date <= t1.date + '5 days'::interval and t1.ln = t2.ln and t1.mbrid = t2.mbrid) from t1 That'll give you both the plus and minus (in two different columns), but it might sum up the same row from table2 multiple times so I'm not sure its correct. And I'm not sure the date range is correct. Another way to look at the same thing: select date, ln, mbrid, dsplus - dsminus from ( select date, ln, mbrid, ds as dsplus, ( select sum(ds) from t2 where t2.date >= t1.date and t2.date <= t1.date + '5 days'::interval and t1.ln = t2.ln and t1.mbrid = t2.mbrid) as dsminus from t1 ) as x where dsplus - dsminus <> 0 Totally guessing here. -Andy ------------------------------------------------------------------- I am pretty certain this cannot be sufficiently solved via a declarative statement; it requires procedural logic. For each unmatched record on table 1 you compare all unmatched records on table 2. You pair the first one that matches and exclude the table 2 record from all future comparisons. I have done this before but my approach was to load all the unmatched records into Java and perform the procedural logic there. This can be done in PL/PGSQL in a brute-force way and then, if performance is unacceptable, you can try to add efficiencies or farm out the processing to a more full featured programming language (one having Lists/Maps and/or Iterators). Two possible situations to consider: 1) Does a record on table 1 (or table 2) ever have to match up with another record on the same table (i.e., entry reversal)? 2) Is it ever possible for a record to be deleted? Also consider what kind of meta-data you want to track in order to generate a proper reconciliation report. One common need is to know what the reconciliation status looked like at some date in the past. For instance on the 5th of the month I want to know the exact reconciliation status of my bank account. To do this I have to ignore any "matching" entries that occurred on or after the 1st of the current month (like checks clearing). Again, the situation you are dealing with almost certainly requires a procedural solution and so pure SQL is not going to work. You need PL/PGSQL (or some other embedded language) or, if you already have an application server hooked into the database, a "query-process-update" routine coded and run off the application server. David J. -----Original Message----- From: Jeff Herman [mailto:hermanj@hvpa.com] Sent: Tuesday, February 21, 2012 4:53 PM To: David Johnston; 'Andy Colson' Cc: pgsql-general@postgresql.org Subject: RE: [GENERAL] Combining data from Temp Tables David, Thanks for that. There is always a feeling of relief and frustration when you learn that a language simply cannot do what you are trying to get it to do. You mentioned that this could be done by brute force with PL/PGSQL. I do have this available, but am somewhat unfamiliar with it and am not sure where to begin. I can answer the two situations you brought up. 1. No, the records do not have to match up with records on the same table. I created the two temp tables as a way of separating the entries with the entry reversals, if that makes sense. Now I am trying to reconcile the tables and take out the appropriate records. 2. It is not possible for a record to be deleted. As for meta-data, I am not too concerned with that at the moment. I am looking just to create a monthly "snapshot" report using this data. Because I am pulling this data from data feeds, I can control any "matching" entries that would occur after the first of the current month. Thank you for considering these things in my problem. That being said, since I am mostly unfamiliar with PL/PGSQL could you (or anyone) provide an example of a solution? I am playing with loops, but I am not sure I am on the right path. --------------------------------------------------------------------- Jeff, I don't have time to put together a full implementation in PL/PGSQL. My first thought would be to open a cursor on the unmatched table 1 entries. For each record you query table 2 for possible matches and use LIMIT 1 to ensure you only retrieve (at most) one match. Insert the ID of the match and the ID of the table 1 record into a table and move onto the next record in the table 1 cursor. When you are done you have a table containing all the matches between tables 1 and 2 and you can: SELECT * FROM table1 WHERE id1 NOT IN (SELECT table1_id FROM matchtable) UNION ALL SELECT * FROM table2 WHERE id2 NOT IN (SELECT table2_id FROM matchtable) to find all of your unmatched entries. Keep in mind concurrency issues and how you plan to handle the monthly nature of the routine (processing and archiving). This should get you started. David J. CREATE FUNCTION reconcile(...) RETURNS TABLE (...) AS $$ DECLARE tbl2_id varchar; BEGIN --PSUEDO CODE BELOW FOR cursor1 IN (SELECT * FROM umatched_table1) SELECT id2 FROM umatched_table2 WHERE (matching conditions) LIMIT 1 INTO tbl2_id; IF (tbl2_id IS NOT NULL) THEN INSERT INTO matchtable (tbl1_id, tbl2_id); END IF; NEXT; RETURN QUERY SELECT * FROM reconciliation_result; RETURN; END; $$ LANGUAGE PLPGSQL STRICT VOLATILE;
pgsql-general by date: