Re: Combining data from Temp Tables - Mailing list pgsql-general
From | Jeff Herman |
---|---|
Subject | Re: Combining data from Temp Tables |
Date | |
Msg-id | DCD5B559B90AFB46B549AFD282BBE42692E459@BY2PRD0410MB388.namprd04.prod.outlook.com Whole thread Raw |
In response to | Re: Combining data from Temp Tables ("David Johnston" <polobo@yahoo.com>) |
Responses |
Re: Combining data from Temp Tables
|
List | pgsql-general |
David, Thanks for that. There is always a feeling of relief and frustration when you learn that a language simply cannot do whatyou are trying to get it to do. You mentioned that this could be done by brute force with PL/PGSQL. I do have thisavailable, but am somewhat unfamiliar with it and am not sure where to begin. I can answer the two situations you broughtup. 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 ofseparating the entries with the entry reversals, if that makes sense. Now I am trying to reconcile the tables and takeout 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" reportusing this data. Because I am pulling this data from data feeds, I can control any "matching" entries that would occurafter 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 amplaying with loops, but I am not sure I am on the right path. Thanks, Jeff Herman HVPA, Database Programmer Phone: 734.973.0137 ext 441 Fax: 734.975.1248 hermanj@hvpa.com -----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 multipletimes 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 andexclude 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 logicthere. This can be done in PL/PGSQL in a brute-force way and then, if performance is unacceptable, you can try to addefficiencies 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 needis to know what the reconciliation status looked like at some date in the past. For instance on the 5th of the monthI want to know the exact reconciliation status of my bank account. To do this I have to ignore any "matching" entriesthat 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 towork. You need PL/PGSQL (or some other embedded language) or, if you already have an application server hooked into thedatabase, a "query-process-update" routine coded and run off the application server. David J.
pgsql-general by date: