写好了吗?代码如下,如果有用,望采纳,谢谢!
with temp as (select acct_no,current_bal,txn_dt,row_number() over(partition by acct_no order by txn_dt) as rn from t03_acctno_bal)
select t1.acct_no as acctno,t1.txn_dt as start_dt,nvl(t2.txn_dt,'3000-12-31') as end_dt,t1.current_bal from temp t1 left join temp t2 on t1.acct_no = t2.acct_no and t1.rn+1=t2.rn
with temp as (select acct_no,current_bal,txn_dt,row_number() over(partition by acct_no order by txn_dt) as rn from t03_acctno_bal)
select t1.acct_no as acctno,t1.txn_dt as start_dt,nvl(t2.txn_dt,'3000-12-31') as end_dt,t1.current_bal from temp t1 left join temp t2 on t1.acct_no = t2.acct_no and t1.rn+1=t2.rn
望采纳!!!点击回答右侧采纳即可!!
with temp as (select acct_no,current_bal,txn_dt,row_number() over(partition by acct_no order by txn_dt) as rn from t03_acctno_bal)
select t1.acct_no as acctno,t1.txn_dt as start_dt,nvl(t2.txn_dt,'3000-12-31') as end_dt,t1.current_bal from temp t1 left join temp t2 on t1.acct_no = t2.acct_no and t1.rn+1=t2.rn
有不懂的可以随时交流
with temp as (select acct_no,current_bal,txn_dt,row_number() over(partition by acct_no order by txn_dt) as rn from t03_acctno_bal)
select t1.acct_no as acctno,t1.txn_dt as start_dt,nvl(t2.txn_dt,'3000-12-31') as end_dt,t1.current_bal from temp t1 left join temp t2 on t1.acct_no = t2.acct_no and t1.rn+1=t2.rn
1、只需要一次全表扫描即可,不需要自关联
WITH t AS (
SELECT '600000001201' acct_no, 800 current_bal, '20120101' txn_dt FROM dual UNION ALL
SELECT '600000001201' acct_no, 900 current_bal, '20120102' txn_dt FROM dual UNION ALL
SELECT '600000001201' acct_no, 400 current_bal, '20120304' txn_dt FROM dual UNION ALL
SELECT '600000001201' acct_no, 520 current_bal, '20120809' txn_dt FROM dual UNION ALL
SELECT '600000001202' acct_no, 100 current_bal, '20120106' txn_dt FROM dual UNION ALL
SELECT '600000001202' acct_no, 200 current_bal, '20120808' txn_dt FROM dual )
SELECT acct_no AS acctno, txn_dt AS start_dt
, NVL( LEAD( txn_dt ) OVER( PARTITION BY acct_no ORDER BY txn_dt ), '30001231' ) end_dt
, current_bal
FROM t;
输出结果:
开窗函数了解一下
使用lead函数 ,详情可以参考下这个 http://t.csdn.cn/EOlOM
select b.acct_no acct_no,
b.txn_dt Start_dt,
lead(b.txn_dt) over(order by b.txn_dt) End_dt,
b.current_bal current_bal
from b;
为了实现这个功能,您可以使用以下SQL语句:
INSERT INTO 下面表 (date, order_id, name, sales)
SELECT t1.date, t1.order_id, t2.name, t1.sales
FROM 上面表 t1
JOIN 关系表 t2 ON t1.order_id = t2.order_id
上面的SQL语句使用了INSERT INTO、SELECT、FROM和JOIN语句,来实现将上面表中的数据“拉链”到下面表中。
在这个SQL语句中,首先使用INSERT INTO语句将数据插入到下面表中,然后使用SELECT语句查询上面表和关系表中的数据,最后使用JOIN语句将两个表关联起来。
希望这些内容能够帮助您解决问题。如果您有任何疑问,请随时告诉我。望采纳。
为了实现这个功能,您可以使用以下SQL语句:
INSERT INTO 下面表 (date, order_id, name, sales)
SELECT t1.date, t1.order_id, t2.name, t1.sales
FROM 上面表 t1
JOIN 关系表 t2 ON t1.order_id = t2.order_id
上面的SQL语句使用了INSERT INTO、SELECT、FROM和JOIN语句,来实现将上面表中的数据“拉链”到下面表中。
在这个SQL语句中,首先使用INSERT INTO语句将数据插入到下面表中,然后使用SELECT语句查询上面表和关系表中的数据,最后使用JOIN语句将两个表关联起来。
希望这些内容能够帮助您解决问题。如果您有任何疑问,请随时告诉我。望采纳。
可参考: https://blog.csdn.net/qq_43514917/article/details/127183266