Question

How to Write Query to Getting Below Output?

In database:

Table 1:

id Company_accountNo
1   123
2   235 
3   456

Table 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

How get output like this?

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

Thanks in advance..

Was it helpful?

Solution

Try This:

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

I Think You are Missing Group By

OTHER TIPS

Try this one:

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
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

Please try:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top