Quote:
|
Originally Posted by mike_bike_kite
Alti
I'm interested how you got it to work in such a small amount of SQL - does it really show the amount paid on the previous date (it looks a bit suspicious to me)?
Mike
|
Well spotted.
After setting up a small test, it appears that my query does in fact not show the correct amount. It show the first amount listed.
It gives the correct Date, but only because it bypasses the sorting by using the MAX function.
I did find a few other ways to achieve the desired results, but the only way that was of any use was:
Code:
SELECT
`first`.`C_ID`,
`first`.`Date`,
`first`.`Amt`
FROM `dbTest` AS `first`
INNER JOIN `dbTest` AS `second`
ON `first`.`C_ID` = `second`.`C_ID`
AND `second`.`Action` = 2
AND `first`.`Action` = 1
WHERE `first`.`O_id` = (
SELECT `third`.`O_id` FROM `dbTest` AS `third`
WHERE `third`.`C_ID` = `first`.`C_ID`
AND `third`.`Action` = 1
ORDER BY `O_id` DESC
LIMIT 1
);
Out of all the queries I tried, this was the only one I found that did not require a temporary table and/or filesort to function properly.
This one and the one you posted should both work fine.
Add an index to the Action column and they will both work slightly faster
