0

我尝试做一个 sql 查询,通过将当前金额乘以所有账户的最低利率,返回客户 ID、他们账户的货币和他们账户的年终金额

表就像

**customer**       **account**
 id                  id
 name                customer_id
 surname             curreny
                     cur_Amount
                     interest_rate

我进行了如下查询,但它只返回具有最低利率的一行。

SELECT customer.id, account.currency, account.cur_amount, MIN(account.interest_rate)*cur_amount AS End_Year_Balance FROM customer, account LEFT JOIN account on customer.id = account.customer_id

如何通过乘以最低利率来列出所有具有切割者 ID 和年终余额的账户?

谢谢

4

4 回答 4

0
SELECT
    customer.id, customer.name, customer.surname,
    account.id, account.currency, account.cur_Amount, account.interest_rate,
    account.cur_Amount * A2.LowestInterestRate

FROM
    customer
    INNER JOIN account ON (customer.id = account.customer_id)
    LEFT JOIN 
        (SELECT customer.id, MIN(interest_rate) LowestInterestRate 
         FROM account 
         GROUP BY customer.id
    ) AS A2 ON (customer.id = A2.customer_id)
于 2012-04-09T10:44:59.397 回答
0

您需要从查询中删除歧义GROUP BY,如果您使用AGGREGATE函数,则应该使用。但是,这是一个适用于类似情况的示例查询。

select a.custid, a.custname, a.surname, 
b.accid, b.currency, b.curamount, b.interestrate, b.EndYearBalance 
from customer a 
left outer join 
(
select accid, custid, currency, curamount, MIN(interestrate) AS interestrate, 
ISNULL(MIN(interestrate)*curamount, 0) AS EndYearBalance 
from  account 
group by accid, custid, currency, curamount   
) as b on a.custid = b.custid 
于 2012-04-09T10:50:07.243 回答
0

这将适用于 MySQL

SELECT min(interest_rate) into @min_interest from account;

SELECT customer.id,account.currency,account.cur_amount,@min_interest*cur_amount AS End_Year_Balance 
FROM customer 
LEFT JOIN account 
ON customer.id = account.customer_id;

这适用于任何数据库:

SELECT customer.id,account.currency,account.cur_amount,temp.min_interest*cur_amount AS End_Year_Balance 
FROM customer 
LEFT JOIN account ON customer.id = account.customer_id
LEFT JOIN (SELECT min(interest_rate) as min_interest from account) temp ON 1=1;
于 2012-04-09T10:37:22.130 回答
0

这应该有效并且非常简单:

SELECT
    c.id,
    a.currency,
    a.cur_amount,
    a.cur_amount * (SELECT MIN(interest_rate) FROM account)
FROM
    customer c left join account a on c.id = a.customer_id
于 2012-04-09T11:12:03.663 回答