简体   繁体   English

普适 SQL 2000i 数据库上次事务查询

[英]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.我有一个旧数据库,名为:Pervasive SQL 2000i,该数据库来自 90 年代末和 2000 年代初。 The documentation is scarce except from Actian(which is called Zen) and from Goldstar Software(which is kind enough to reply to my queries).文档很少,除了来自 Actian(称为 Zen)和 Goldstar Software(很友好地回答我的问题)。 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.这个问题可能适用于所有数据库,因为这只是一个标准的 SQL 语句,但是对于我来说,我仍然很难在单个 SQL 语句上检索数据。

I have a Pervasive table that looks like this:我有一个看起来像这样的 Pervasive 表:

CREATE TABLE `NLBAL` (
    `acct` VARCHAR(32),
    `PerioEndDate` DATE,
    `Amt` DECIMAL,
    `YTDAmt` DECIMAL
);

(create table courtesy of wtools ) (创建表由 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:如果我使用这个 SQL 语句:

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):当然,结果仅基于“最大”日期,将忽略其他帐户的先前日期,它将显示所有具有等效 = max 日期的记录,并忽略那些没有的记录(预期结果):

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.如我们所见,我的查询无法从帐户“01212121221220”中获取结果,该帐户具有 2017 年的最后记录交易,并且由于 2020-10-31 是表上的最新记录,数据库将使用该日期过滤结果.

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.我只想查询所有账户及其对应的最后一笔交易(1 个记录),无论日期是什么,只要它是每个账户的最后一笔交易。 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.将结果导出到包含 Acct、PeriodEndDate 列的新表“ACCTPRD”。

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.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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