2014-04-29 89 views
0

如何編寫查詢以獲取下面的輸出?如何從數據庫中獲取每個總數?

在數據庫:

表1:

id Company_accountNo 
1 123 
2 235 
3 456 

表2:

id cheque_no company_accnopky amount 
1 258  1    100 
2 963  1    200 
3 147  2    500 
4 148  3    800 
5 852  2    300 

如何獲取輸出這樣嗎?

Account_no Total_Amount No_of_Cheque 
123   300    2 
235   800    2 
456   800    1 

在此先感謝..

+0

?你還可以添加你迄今爲止嘗試過的嗎? –

+0

我正在使用oracle 9i – sara

+2

MSSQL/MySQL語法:'select company_acc_no,count(1)as no_of_cheque,sum(amount)as total_amount from table1 inner join table2 on table1.company_acc_np = table2.company_acc_no group by company_acc_no' – mshsayem

回答

1

試試這個:

SELECT 
T1.Company_accountNo As Account_no, 
sum(T2.Amount) As Total_Amount, 
count(T1.Company_accountNo) AS No_of_Cheque 
FROM Table1 T1 Inner Join Table2 T2 ON T1.id = T2.company_accnopky 
Group By T1.Company_accountNo 

我認爲你是缺少集團通過您正在使用什麼數據庫管理系統

+0

是的你是對的我忘了把組合.. – sara

1
SELECT a.Company_accountNo Account_no, 
    SUM(b.amount) Total_Amount, 
    COUNT(*) No_of_Cheque 
FROM table1 a 
LEFT JOIN table2 b 
ON a.id=b.company_accnopky 
GROUP BY a.Company_accountNo 
0

請嘗試:

select 
    a.Company_accountNo as Account_no, 
    SUM(b.amount) as Total_Amount, 
    COUNT(*) No_of_Cheque 
from 
    tbl1 a inner join tbl2 b on a.id=b.company_accnopky 
group by a.Company_accountNo 
1

試試這個:

select t1.Company_accountNo, SUM(t2.amount) as amount, count(t2.cheque_no) as cheque_no 
from table1 t1, table2 t2 
where t1.id=t2.company_accnopky 
group by t1.Company_accountNo 
相關問題