Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

oracle - How to select rows with same dates from two tables in pl sql

I have two tables with a common column "hist_date". I want the rows with common dates only.

Client_table

+-----------+--------+-----------+
| hist_date | payable| receivable|
+-----------+--------+-----------+
|03-Jan-2021| 1200.00|       0.00|
|04-Jan-2021| 1300.00|       0.00|
|05-Jan-2021| 2500.00|       0.00|
|06-Jan-2021|    0.00|    1653.00|
|07-Jan-2021|    0.00|    1452.34|
+-----------+--------+-----------+

GL_table

+-----------+-----------+--------------+
| hist_date | gl_payable| gl_receivable|
+-----------+-----------+--------------+
|03-Jan-2021|    1200.00|          0.00|
|04-Jan-2021|    1300.00|          0.00|
|06-Jan-2021|       0.00|       1653.00|
|07-Jan-2021|       0.00|       1452.34|
+-----------+-----------+--------------+

Now I want the result to be

+-----------+-----------+--------------+--------+-----------+
| hist_date | gl_payable| gl_receivable| payable| receivable|
+-----------+-----------+--------------+--------+-----------+
|03-Jan-2021|    1200.00|          0.00| 1200.00|       0.00|
|04-Jan-2021|    1300.00|          0.00| 1300.00|       0.00|
|06-Jan-2021|       0.00|       1653.00|    0.00|    1653.00|
|07-Jan-2021|       0.00|       1452.34|    0.00|    1452.34|
+-----------+-----------+--------------+--------+-----------+

Can anyone please provide the correct query?

EDIT:

I'm sorry I should have mentioned that the resultant tables are already derived from a complex query.

SELECT A.hist_date,SUM(A.payable),SUM(A.gl_payable),SUM(A.payable)-SUM(A.gl_payable) payable_diff,SUM(A.receivable),SUM(A.gl_recievable),SUM(A.receivable)-SUM(A.gl_recievable) receivable_diff
FROM (
    select h.hist_date,SUM(h.current_balance) payable,0 gl_payable,0 receivable,0 gl_recievable
    from investor_financials_hist h
    inner join investor_accounts ia on ia.account_id=h.account_id
    where ia.product_id in (1,2)
    and ia.investor_code not like 'F%'
    and ia.investor_code not like 'A%'
    and h.current_balance>0
    group by h.hist_date
    UNION ALL
    select h.hist_date,0 payable,0 gl_payable,SUM(h.current_balance) receivable,0 gl_recievable
    from investor_financials_hist h
    inner join investor_accounts ia on ia.account_id=h.account_id
    where ia.product_id in (1,2)
    and ia.investor_code not like 'F%'
    and ia.investor_code not like 'A%'
    and h.current_balance<0
    group by h.hist_date
    UNION ALL
    select g.trans_dt hist_date,0 payable,0 gl_payable,0 receivable,PKG_USL.GET_GL_OPENING_BALANCE(g.ac_cat,g.ac_no,g.trans_dt)+SUM(decode(g.dr_cr,'DR',g.amount_ccy))-SUM(decode(g.dr_cr,'CR',g.amount_ccy)) gl_recievable
    from gl_trans_hist g
    where g.ac_no='0102090101000'
    group by g.trans_dt,g.ac_cat,g.ac_no
    UNION ALL
    select g.trans_dt hist_date,0 payable,PKG_USL.GET_GL_OPENING_BALANCE(g.ac_cat,g.ac_no,g.trans_dt)+SUM(decode(g.dr_cr,'CR',g.amount_ccy))-SUM(decode(g.dr_cr,'DR',g.amount_ccy)) gl_payable,0 receivable,0 gl_recievable
    from gl_trans_hist g
    where g.ac_no='0201060100000'
    group by g.trans_dt,g.ac_cat,g.ac_no
)A
GROUP BY A.hist_date
ORDER BY A.hist_date 
question from:https://stackoverflow.com/questions/65896418/how-to-select-rows-with-same-dates-from-two-tables-in-pl-sql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like a straightforward join, no tricks at all.

  SELECT c.hist_date,
         g.gl_payable,
         g.gl_receivable,
         c.payable,
         c.receivable
    FROM client_table c JOIN gl_table g ON g.hist_date = c.hist_date
ORDER BY c.hist_date

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...