I have an interesting database problem.
IF you open the attached txt file of some sample data it will help. (help me :P)
First lot shows current SRATIO, the second table is a history of SRATIO with the date of change. The problem is that in SOME records the 2 values dont match up. The current SR is not equal to the latest change of SR in the other table, such as 105848. Besides needing to find out how this happened, I need to fix up the values.
Can someone tell me the SQL needed to compare the 2. I need to select the latest change in the second table and match it to the same link no in the first table.
much appreciated.
[ Edited on 12-2-2006 by amazeer ]
[ Edited on 12-2-2006 by amazeer ]
There are many ways to do it. Here's one whicj is step-wise; each step can be done in standard SQL, 'though it might be quicker to use the toolset of your choice:
1. Create a table for the changes (and import them) with 3 fields; let's call it "changes"
2. Copy its structure (only) to a new table; let's call it "changes_struct"
3. In changes_struct, set LINK_NUMBER as primary key
4. Sort changes to e.g. "changes_sorted"
SELECT changes.LINK_NUMBER, changes.SRATIO, changes.CREATED_DATETIME
FROM changes
ORDER BY changes.LINK_NUMBER, changes.CREATED_DATETIME DESC;
5. Append from changes_sorted to changes_struct; only the first, unique values will come across:
INSERT INTO changes_structure
SELECT changes_sorted.*
FROM changes_sorted;
6. Create a table for latest values (and import them); let's call it "latest"
7. Update latest from changes_struct:
UPDATE latest, changes_structure SET latest.SRATIO = [changes_structure].[SRATIO]
WHERE (((latest.LINK_NUMBER)=[changes_structure].[LINK_NUMBER]));
Done. I am sure it could be done in about 10 lines of Perl and would be more transparent than the above.
hth
Peter Hill
1975 Type 2 Double Cab
modulus.com.au