I have an old database called: Pervasive SQL 2000i, this database is from the late 90s and early 2000s. The documentation is scarce except from Actian(which is called Zen) and from Goldstar Software(which is kind enough to reply to my queries). This post is a question about how to query a data from each different period. This question maybe applicable to all databases since this is just a standard SQL statement, but for the life of me, I still have a hard time how to retrieve data on a single SQL statement.
I have a Pervasive table that looks like this:
CREATE TABLE `NLBAL` (
`acct` VARCHAR(32),
`PerioEndDate` DATE,
`Amt` DECIMAL,
`YTDAmt` DECIMAL
);
(create table courtesy of wtools )
Then part of the data:
Acct PeriodEndDate Amt YTDAmt
01212121221220 2017-11-30 -5000 40000
01212121221220 2017-12-31 -5000 40000
12010111111111 2020-09-31 -4000 12000
12010111111111 2020-10-31 1000 80000
If I use this SQL statement:
SELECT Acct,PeriodEndDate,Amt,YTDAmt FROM NLBAL
WHERE PeriodEndDate = (SELECT Max(PeriodEndDate) FROM NLBAL)
And of course, the results are based only on the "maximum" date and will ignore the previous dates of other accounts, it will display all records that has the equivalent = max date and will ignore those who does not (expected result):
Acct PeriodEndDate Amt YTDAmt
12010111111111 2020-10-31 1000 80000
As we can see, my query cannot get the result out from the account "01212121221220" which has a last recorded transaction from 2017 and since 2020-10-31 is the latest record on the table, the db will use that date to filter results.
I want the result to be:
Acct PeriodEndDate Amt YTDAmt
01212121221220 2017-12-31 -5000 40000
12010111111111 2020-10-31 1000 80000
I just want to query all of the accounts and their corresponding last transaction(1 rec) regardless of what date as long it's the last transaction of each account. What this means is that the results need to have just one record for each and every account where a recorded transaction is the last one based from maximum/latest date.
How do I do this?
Thank you for any advice.