[英]Copy and pick up previous records from table
I have transactions and reservations table.我有交易和预订表。 I have to join and fetch checkin and checkout data from reservations.
我必须加入并从预订中获取签到和结帐数据。 But one issue I have is if there is a change only in the reservations, i have to copy the transactions to a new row and change fiscal period to changeDate and add new checkin-checkout date.
但是我遇到的一个问题是,如果仅保留更改,我必须将交易复制到新行并将会计期间更改为 changeDate 并添加新的签到-签出日期。
Reservations:预订:
I tried with the query but not sure how to pick up the records from other table from previous rows:我尝试了查询,但不确定如何从前几行的其他表中获取记录:
SELECT IFNULL(a."fiscalPeriod","changeDate") as "fiscalPeriod"
,b."purchaseId","charge","refund","checkIn","checkOut"
FROM reservations a
FULL OUTER JOIN transactions b
ON a."purchaseId"=b."purchaseId"
and b."fiscalPeriod" >= "validfrom"
and b."fiscalPeriod" < "validTo"
Output: Output:
You could use FIRST_VALUE analytic function with windowing clause considereing just 1 previous and current row.您可以使用 FIRST_VALUE 分析 function 和窗口子句,只考虑前一行和当前行。 There were some other issues in your SQL.
您的 SQL 中还有其他一些问题。 Anyway here is the code with the result.
无论如何,这是带有结果的代码。 The WITH clause is here just to generate sample data and, as such, it is not a part of the answer
WITH 子句在这里只是为了生成示例数据,因此,它不是答案的一部分
WITH
transactions AS
(
Select 2022002 "FISC_PERIOD", 'P1' "PURCHASE_ID", 100 "CHARGE", 50 "REFUND" From Dual
),
reservations AS
(
Select 'P1' "PURCHASE_ID", 2022001 "VALID_FROM", 2022003 "VALID_TO", 2022004 "CHECK_IN", 2022005 "CHECK_OUT", 2022001 "CHANGE_DATE" From Dual Union All
Select 'P1' "PURCHASE_ID", 2022003 "VALID_FROM", 9999012 "VALID_TO", 2022011 "CHECK_IN", 2022012 "CHECK_OUT", 2022003 "CHANGE_DATE" From Dual
)
SELECT
Nvl(a.FISC_PERIOD, b.CHANGE_DATE) "FISC_PERIOD",
b.PURCHASE_ID "PURCHASE_ID",
Nvl(a.CHARGE, FIRST_VALUE(a.CHARGE) OVER(PARTITION BY b.PURCHASE_ID ORDER BY b.PURCHASE_ID, b.VALID_FROM ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)) "CHARGE",
Nvl(a.REFUND, FIRST_VALUE(a.REFUND) OVER(PARTITION BY b.PURCHASE_ID ORDER BY b.PURCHASE_ID, b.VALID_FROM ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)) "REFUND",
b.CHECK_IN "CHECK_IN",
b.CHECK_OUT "CHECK_OUT"
FROM reservations b
LEFT JOIN transactions a
ON (a.PURCHASE_ID = b.PURCHASE_ID and
a.FISC_PERIOD >= b.VALID_FROM and
a.FISC_PERIOD < b.VALID_TO)
--
-- R e s u l t
--
-- FISC_PERIOD PURCHASE_ID CHARGE REFUND CHECK_IN CHECK_OUT
-- ----------- ----------- ---------- ---------- ---------- ----------
-- 2022002 P1 100 50 2022004 2022005
-- 2022003 P1 100 50 2022011 2022012
Here is reservations table in the FROM clause and LEFT JOIN to transactons on the same conditions.这是 FROM 子句中的保留表和 LEFT JOIN 到相同条件下的事务。 When selecting data from transactions (CHARGE, REFUND) if value is null there is a FIRST_VALUE analytic function (considering just 1 previous and current row) to get you the value from the preceding row.
从事务(CHARGE、REFUND)中选择数据时,如果值为 null,则有一个 FIRST_VALUE 分析 function(仅考虑前一行和当前行)从前一行中获取值。 Regards...
问候...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.