2012-02-17 34 views
0

我有三个表的客户,customer_account,account_transaction需要帮助得到下面的查询

表结构如下 -

客户

Id, 
Branch, 
Name 

..

customer_account

Id, 
Cust_id, 
Loanamout, 
EMI 

account_transaction

ID, 
account_id, 
amount, 
date 

我需要在给定的贷款的数量,给予贷款总和的形式分支明智的细节,并为特定分支收到EMI的总和。以下是我当前的查询 -

SELECT 
    count(s.id) as cntloan, 
    SUM(s.Loanamout) 
    (
    SELECT SUM(amount) 
    FROM account_transaction i 
    WHERE s.id = i.account_id 
) AS curbal 
From 
    customer as c, 
    customer_account as s 
where c.branch = 1 and s.cust_id = c.id 

它给我所期望的结果贷款计数和贷款的总和。但没有给出由顾客支付的EMI的正确金额

任何人都可以帮助我解决这个问题。

非常感谢你

+2

我在该查询中看不到EMI。你在哪里工作/检索它? – 2012-02-17 13:37:48

+0

这是期中考试吗?这是一个非常基本的数据库类问题。 – Churk 2012-02-17 13:38:17

+0

格式化时,我注意到一个缺少的逗号让子查询感到厌烦。我们认为这是一个错字... – 2012-02-17 13:39:14

回答

0

此SQL具有聚集功能,如数量和金额,所以没有一组通过,这是行不通的。

SELECT customer.id, 
COUNT(customer_account.id) as cntloadn, 
SUM(customer_account.loan) as loan, 
SUM(account_transaction.amount) 
FROM customer 
JOIN customer_account ON customer_account.cust_id = customer.id 
JOIN account_transaction ON account_transaction.account_id = customer_account.id 
GROUP BY customer.id