[英]iSeries SQL - Use Previous Record Value in JOIN criteria
In my example, I am trying to retrieve records from a transaction file based on the previous calendar work date.在我的示例中,我试图根据以前的日历工作日期从事务文件中检索记录。 The work dates are in a file called CALNDR, the transactions in a file called TRNHST.
工作日期在一个名为 CALNDR 的文件中,交易在一个名为 TRNHST 的文件中。 The logic would be to use the CURRENT_DATE to set the cursor on CALNDR, then retrieve the previous work date which will be used to join to TRNHST.
逻辑是使用 CURRENT_DATE 在 CALNDR 上设置 cursor,然后检索将用于加入 TRNHST 的先前工作日期。 This would be simple enough if done in RPG but I am at a loss with SQL.
如果在 RPG 中完成,这将很简单,但我对 SQL 不知所措。 Thanks, for any input you can provide.
谢谢,您可以提供任何输入。
This is even more simple with SQL probably.这可能更简单 SQL 。
You may uncomment the commented out block to run it as is to check.您可以取消注释已注释掉的块以按原样运行它以进行检查。
/*
WITH
CALNDR (DT, IS_WORK) AS
(
VALUES
(CURRENT_DATE - 1 DAY, 'N')
, (CURRENT_DATE - 2 DAY, 'Y')
, (CURRENT_DATE - 3 DAY, 'Y')
)
, TRNHST (DT, SOME_COL) AS
(
VALUES
(CURRENT_DATE - 1 DAY, 1)
, (CURRENT_DATE - 2 DAY, 2)
, (CURRENT_DATE - 3 DAY, 3)
)
*/
SELECT CURRENT_DATE AS CDT, T.*
FROM
(
SELECT MAX (DT) DT
FROM CALNDR
WHERE DT < CURRENT_DATE AND IS_WORK = 'Y'
) C
JOIN TRNHST T ON T.DT = C.DT
Honestly, if "prior working day" for a given date is something you need.老实说,如果您需要给定日期的“前工作日”。 You should consider adding such a column to your calendar table.
您应该考虑将这样的列添加到您的日历表中。
While you're at it, might as well add a "next working day".当您使用它时,不妨添加一个“下一个工作日”。
One of the biggest benefits to a calendar table is the ability to pre-calculate such columns.日历表的最大好处之一是能够预先计算这些列。 Thus, greatly simplifying your statements.
因此,大大简化了您的陈述。
Not real sure what LAG does as I have never used it.不确定 LAG 是做什么的,因为我从未使用过它。 So, here's another solution using a cross join:
因此,这是使用交叉连接的另一种解决方案:
WITH calendar (workdt) AS (
VALUES (CAST('2022-01-01' AS date)),
(CAST('2022-02-01' AS date)),
(CAST('2022-03-01' AS date)),
(CAST('2022-04-01' AS date))
),
orders (orderno, orderdt) AS (
VALUES (1, CAST('2022-01-15' AS date)),
(2, CAST('2022-02-15' AS date)),
(3, CAST('2022-03-15' AS date)),
(4, CAST('2022-04-15' AS date))
)
SELECT *
FROM orders a
CROSS JOIN LATERAL (SELECT max(workdt) prevdt FROM calendar WHERE workdt < a.orderdt) b
The cross join only returns a single row for each record in the calendar file, and that row contains the largest work date less than the order date.交叉连接只为日历文件中的每条记录返回一行,并且该行包含小于订单日期的最大工作日期。 LATERAL just lets the sub-query access columns from the outer query.
LATERAL 只是让子查询访问外部查询中的列。
write an SQL function named calndr_prevWorkDate
that returns the previous work date.编写一个名为
calndr_prevWorkDate
的 SQL function 返回上一个工作日期。
Then use that function in the WHERE
clause of an SQL SELECT
statement to select records from the TRNHST
table: Then use that function in the
WHERE
clause of an SQL SELECT
statement to select records from the TRNHST
table:
select a.*
from trnhst a
where a.trandate = calndr_prevWorkDate( )
Here is the SQL function.这是 SQL function。
CREATE OR REPLACE FUNCTION calndr_prevWorkDate(
startDate date default current date )
RETURNS date
language sql
specific calndrf1
SET OPTION datfmt = *ISO, DLYPRP = *YES, DBGVIEW = *SOURCE,
USRPRF = *OWNER, DYNUSRPRF = *OWNER, COMMIT = *CHG
BEGIN
declare vCalDate date ;
declare vSqlCode decimal(5,0) default 0 ;
declare sqlCode int default 0 ;
declare sqlState char(5) default ' ' ;
declare vSqlState char(5) default ' ' ;
declare vErrText varchar(256) default '' ;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
begin
SET vSqlCode = SQLCODE ;
SET vSqlState = SQLstate ;
get diagnostics exception 1 vErrText = message_text ;
end ;
select a.caldate
into vCalDate
from calndr a
where a.caldate < startDate
and dayofweek(a.caldate) not in ( 1, 7)
order by a.caldate desc
fetch first row only ;
return vCalDate ;
END
if all u need is the previous date from a table you can use something like that如果您只需要表格中的上一个日期,您可以使用类似的东西
select * from TRNHST where Trans_Date = ( select calendar_Date from CALNDR where calendar_Date < current date order by calendar_Date desc limit 1 ) select * 来自 TRNHST where Trans_Date = ( select calendar_Date from CALNDR where calendar_Date < current date order by calendar_Date desc limit 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.