0% found this document useful (0 votes)
17 views2 pages

Billing Split Query (With Functions)

The SQL query retrieves contract-related financial data, including rent, insurance receivables, interest, and residual value, grouped by contract ID and number, and filtered by specific conditions. It combines data from multiple tables and applies various conditions to ensure only relevant records are included, particularly those active as of October 31, 2023. The results are ordered by contract number and stream element date.

Uploaded by

lightstar10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Billing Split Query (With Functions)

The SQL query retrieves contract-related financial data, including rent, insurance receivables, interest, and residual value, grouped by contract ID and number, and filtered by specific conditions. It combines data from multiple tables and applies various conditions to ensure only relevant records are included, particularly those active as of October 31, 2023. The results are ordered by contract number and stream element date.

Uploaded by

lightstar10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

SELECT

x.contract_ID,
x.contract_number,
to_char(x.stream_element_date, 'dd-mon-yyyy') stream_element_date,
nvl(SUM(decode(x.code, 'RENT', x.amount)), 0) rent,
nvl(SUM(decode(x.code, 'INSURANCE RECEIVABLE', x.amount)), 0) insurance,
nvl(SUM(decode(x.code, 'PRE-TAX INCOME', x.amount)), 0) interest,
nvl(SUM(decode(x.code, 'RESIDUAL VALUE', x.amount)), 0) residual_value
FROM
(SELECT okh.id contract_ID,
okh.contract_number,
sty.code,
sel.date_billed,
sel.stream_element_date,
sel.amount,
sel.accrued_yn,
okll.date_terminated,
os.date_current,
os.date_history
FROM
OKL.okl_strm_elements sel,
OKL.okl_streams os,
OKL.okl_strm_type_b sty,
OKC.okc_k_headers_all_b okh,
OKC.okc_k_lines_b oklv,
OKC.okc_k_lines_b okll,
OKL.okl_txl_assets_b asst
WHERE
sel.stm_id = os.id
AND os.sty_id = sty.id
AND os.kle_id = okll.cle_id
AND okh.id = os.khr_id
AND okh.id = oklv.chr_id
AND okll.cle_id = oklv.id
AND asst.kle_id = okll.id
AND sty.code IN (
'RENT',
'INSURANCE RECEIVABLE',
'PRE-TAX INCOME',
'RESIDUAL VALUE'
)
AND os.date_current = ( SELECT MAX(oss.date_current)
FROM OKL.okl_streams oss
WHERE oss.kle_id = okll.cle_id
AND os.sty_id=oss.sty_id
AND oss.date_current<=nvl(('31-Oct-
2023'), sysdate)
)
and (os.date_history = ( SELECT Min(oss.date_history)
FROM OKL.okl_streams oss
WHERE oss.kle_id = okll.cle_id
AND os.sty_id=oss.sty_id
AND oss.date_history>nvl(('31-Oct-2023'),
sysdate)
)
or os.date_history is null
)
and (XXAMG_RY_OLFM_UTIL_PKG.get_termination_date(okh.id,oklv.id) is
null or XXAMG_RY_OLFM_UTIL_PKG.get_termination_date(okh.id,oklv.id) >= nvl(('31-
Oct-2023'), sysdate))
UNION ALL
SELECT okh.id contract_ID,
okh.contract_number,
sty.code,
sel.date_billed,
sel.stream_element_date,
sel.amount,
sel.accrued_yn,
okh.date_terminated,
os.date_current,
os.date_history
FROM
OKL.okl_strm_elements sel,
OKL.okl_streams os,
OKL.okl_strm_type_b sty,
OKC.okc_k_headers_all_b okh
WHERE
sel.stm_id = os.id
AND os.sty_id = sty.id
AND os.kle_id IS NULL
AND okh.id = os.khr_id
AND sty.code IN (
'INSURANCE RECEIVABLE'
)
AND os.date_current = ( SELECT MAX(oss.date_current)
FROM OKL.okl_streams oss
WHERE os.kle_id IS NULL
AND okh.id = oss.khr_id
AND os.sty_id=oss.sty_id
AND oss.date_current<nvl(('31-Oct-
2023'), sysdate)
)
AND (os.date_history = ( SELECT Min(oss.date_history)
FROM OKL.okl_streams oss
WHERE os.kle_id IS NULL
AND okh.id = oss.khr_id
AND os.sty_id=oss.sty_id
AND oss.date_history>nvl(('31-Oct-
2023'), sysdate)
)
or os.date_history is null
)) x,
OKC.okc_k_headers_all_b okh
WHERE x.contract_id=okh.id
AND okh.sts_code NOT IN ('ABANDONED','INCOMPLETE','APPROVED','PASSED')
AND ( ( xxamg_ry_olfm_util_pkg.get_booking_date(okh.id) <= ('31-Oct-2023') ) OR
(('31-Oct-2023') IS NULL ) )
AND (XXAMG_RY_OLFM_UTIL_PKG.get_termination_date(okh.id)>NVL(('31-Oct-
2023'),SYSDATE) or XXAMG_RY_OLFM_UTIL_PKG.get_termination_date(okh.id) is null)

GROUP BY
x.contract_ID,
x.contract_number,
x.stream_element_date
ORDER BY
x.contract_number,
x.stream_element_date

You might also like