2017-07-26 35 views
0

我有充分的帳號和期/貸款條件(貸款期限爲個月)的表達到特定值填充編號行,直到與另一列

我需要做的就是填充編號行對於每個小於或等於貸款期限的帳號。我已經把它貼在下面的屏幕截圖:

Example

因此,對於這個具體的例子,我將需要48個這個帳號數行,因爲該術語僅48個月。

感謝您的幫助!

+0

什麼是你的數據庫版本? (如12.1.0.2.0)如果你不確定,'select * from v $ version'。不同版本的數據庫中有不同的答案。 – mathguy

+0

請發佈格式化文本[不是圖像](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557# 285557)。 –

+0

Oracle版本:11g企業版版本11.2.0.3。 – DieHard345

回答

1
with 
    test_data (account_nmbr, term) as (
     select 'ABC200', 6 from dual union all 
     select 'DEF100', 8 from dual 
    ) 
-- End of simulated inputs (for testing purposes only, not part of the solution). 
-- SQL query begins BELOW THIS LINE. 
select  level as row_nmbr, term, account_nmbr 
from  test_data 
connect by level <= term 
     and prior account_nmbr = account_nmbr 
     and prior sys_guid() is not null 
order by account_nmbr, row_nmbr -- If needed 
; 

    ROW_NMBR  TERM ACCOUNT_NMBR 
    -------- ---------- ------------ 
     1   6 ABC200 
     2   6 ABC200 
     3   6 ABC200 
     4   6 ABC200 
     5   6 ABC200 
     6   6 ABC200 
     1   8 DEF100 
     2   8 DEF100 
     3   8 DEF100 
     4   8 DEF100 
     5   8 DEF100 
     6   8 DEF100 
     7   8 DEF100 
     8   8 DEF100 

在Oracle 12,可以使用LATERAL子句相同:

with 
    test_data (account_nmbr, term) as (
     select 'ABC200', 6 from dual union all 
     select 'DEF100', 8 from dual 
    ) 
-- End of simulated inputs (for testing purposes only, not part of the solution). 
-- SQL query begins BELOW THIS LINE. 
select l.row_nmbr, t.term, t.account_nmbr 
from  test_data t, 
     lateral (select level as row_nmbr from dual connect by level <= term) l 
order by account_nmbr, row_nmbr -- If needed 
; 
+0

第二個查詢在11g中完美工作。非常感謝你做的這些! – DieHard345

+0

@ DieHard345 - 你的意思是**第一**查詢在11g工作?如果第二個查詢確實有效,那將是非常令人驚訝的,因爲'LATERAL'僅在Oracle 12.1中引入(或至少有文檔記載)。 – mathguy

+0

橫向查詢在11g數據庫中工作我正在運行此。我甚至從v $版本運行select *來確認版本。再次感謝您的幫助。 – DieHard345