2015-09-02 24 views
0

的數據結構:編輯共線1

sql-> desc t1

- List item 
    p_code number, 
    acc_date date , 
    debit number, 
    credit number 

數據我的表內:

sql-> select * from t1;

| p_code | acc_date | debit | credit | 
| 001 | 01-01-15 | 100 | 25  | 
| 001 | 02-01-15 | 0  | 125 | 
| 001 | 03-01-15 | 415 | 85  |   

我想這樣做這個:

select * from t1 
where acc_date between :fromdate and :todate 
union all 
    select p_code, (sum(nvl(debit,0))- sum(nvl(credit,0))) open_balance 
    from t1 
    where acc_date < :fromdate 
; 

但是,我無法弄清楚我的錯誤是什麼。

回答

3

類型和union列數必須是相同的 -

select p_code, acc_date, debit, credit, null as open_balance 
    from t1 
where acc_date between :fromdate and :todate 
union all 
select p_code, null as acc_date, null as debit, null as credit, 
     (sum(nvl(debit, 0)) - sum(nvl(credit, 0))) open_balance 
    from t1 
where acc_date < :fromdate 
+0

感謝主席先生,我試試這個 –

+0

它的工作............感謝... –

+0

請幫助...報告加上每次在debit_acc中使用open_balance + - credit_acc = –