简体   繁体   中英

Pervasive SQL 2000i Database Last Transaction Query

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.

Ok, I ended up doing this:

SELECT Acct,Max(PeriodEndDate) as PeriodEndDate FROM NLBAL     
WHERE ACCT IN
('01212121221220','12010111111111')
GROUP BY Acct --(I actually have more than 600 records to query.)

Exported the results to a new table "ACCTPRD" with Acct,PeriodEndDate columns.

Then I use this query:

SELECT DISTINCT Acct,PeriodEndDate,Amt,YTDAmt FROM NLBAL 
WHERE PeriodEndDate in (SELECT PeriodEndDate FROM ACCTPRD WHERE Acct=NLBAL.Acct)

Then I've got the results I wanted. However, I am open to suggestion that is more optimal than what I have here.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM