MySQL:如何在WHERE子句中使用MAX函数?

时间:2018-02-07 05:28:29

标签: mysql sql

这是我的查询生成的。

LOANTYPE            | TOTALBALANCE | STATUS |  
--------------------|--------------|--------|  
Conventional Loans  | 52.84        | Active |  
Conforming Loans    | 45.55        | Active |  
Non-Conforming Loans| 43.90        | Active |   
Secured Loans       | 42.73        | Active |   
Unsecured Loans     | 34.99        | Active |  
Open-ended Loans    | 11.99        | Active |  
Close-ended Loans   | 11.69        | Active |  
  

TOTALBALANCE列是每LOANTYPE个有效帐户和非活动帐户的总和。

这是我的查询

SELECT
  product.LOANTYPE,
  SUM(account.PRINCIPALBALANCE + account.INTERESTBALANCE) AS TOTALBALANCE,
  IF(DATE_ADD(MAX(transaction.PAYMENTDATES),
    INTERVAL 6 MONTH) > CURRENT_DATE(),
    'Active',
    'Innactive') AS LOANSTATUS
FROM
  account
    INNER JOIN
  client ON account.ACCOUNTKEY = client.PRIMARYKEY
    INNER JOIN
  product ON account.PRODUCTKEY = product.PRIMARYKEY
    INNER JOIN
  transaction ON transaction.ACCOUNTKEY = loanaccount.PRIMARYKEY
WHERE
  transaction.TYPE = 'REPAYMENT' 
GROUP BY product.LOANTYPE

我希望得到TOTALBALANCE个有效/无效帐户,以及每个LOANTYPE有多少有效/无效。

LOANTYPE            | ACTIVEBALANCE | ACTIVE# | INACTIVEBALANCE | INNACTIVE#  
--------------------|---------------|---------|-----------------|------------ 
Conventional Loans  | 35.23         | 2       | 17.61           | 1  
Conforming Loans    | 18.22         | 1       | 27.33           | 1  
Non-Conforming Loans| 32.486        | 2       | 11.414          | 2   
Secured Loans       | 17.092        | 2       | 25.638          | 1   
Unsecured Loans     | 40.61         | 2       | 5.6112          | 1  
Open-ended Loans    | 7.194         | 1       | 4.796           | 1  
Close-ended Loans   | 6.4395        | 2       | 5.26            | 2  

我添加了     WHERE子句中的AND DATE_ADD(MAX(transaction.PAYMENTDATES),INTERVAL 6 MONTH) > CURRENT_DATE() 限制结果,但它仍然给我错误。

在我的查询中我应该/应该修改什么才能让我的想法发生。 谢谢你的时间。

2 个答案:

答案 0 :(得分:1)

使用条件聚合。下面的基本思想是我们使用CASE表达式有条件地取一笔余额或记录计数,具体取决于记录是分类为活动还是非活动。

SELECT
    product.LOANTYPE,
    SUM(CASE WHEN DATE_ADD(t.PAYMENTDATES, INTERVAL 6 MONTH) > CURRENT_DATE()
             THEN account.PRINCIPALBALANCE + account.INTERESTBALANCE
             ELSE 0 END) AS ACTIVEBALANCE,
    COUNT(CASE WHEN DATE_ADD(t.PAYMENTDATES, INTERVAL 6 MONTH) > CURRENT_DATE()
               THEN 1 END) AS ACTIVE_CNT,
    SUM(CASE WHEN DATE_ADD(t.PAYMENTDATES, INTERVAL 6 MONTH) <= CURRENT_DATE()
             THEN account.PRINCIPALBALANCE + account.INTERESTBALANCE
             ELSE 0 END) AS INACTIVEBALANCE,
    COUNT(CASE WHEN DATE_ADD(t.PAYMENTDATES, INTERVAL 6 MONTH) <= CURRENT_DATE()
               THEN 1 END) AS INACTIVE_CNT
FROM account a
INNER JOIN client c
    ON a.ACCOUNTKEY = c.PRIMARYKEY
INNER JOIN product p
    ON a.PRODUCTKEY = p.PRIMARYKEY
INNER JOIN
(
    SELECT ACCOUNTKEY, MAX(PAYMENTDATES) AS PAYMENTDATES
    FROM transaction
    WHERE TYPE = 'REPAYMENT'
    GROUP BY ACCOUNTKEY
) t
    ON t.ACCOUNTKEY = a.PRIMARYKEY
GROUP BY
    p.LOANTYPE;

请注意,在您的问题中,您引用的是loanaccount表,但此表在实际查询中不会出现。我假设您打算将其引用到account表。

答案 1 :(得分:0)

这个怎么样

SELECT product.loantype, 
       active.totalbalance   AS ACTIVEBALANCE, 
       active.cnt            AS ACTIVE, 
       incative.totalbalance AS INACTIVEBALANCE, 
       inactive.cnt          AS INACTIVE 
FROM   product 
       left join (SELECT product.loantype, 
                         SUM(account.principalbalance 
                             + account.interestbalance) AS TOTALBALANCE, 
                         COUNT(1)                       AS cnt 
                  FROM   account 
                         inner join client 
                                 ON account.accountkey = client.primarykey 
                         inner join product 
                                 ON account.productkey = product.primarykey 
                  WHERE  TRANSACTION.TYPE = 'REPAYMENT' 
                         AND Current_date() > (SELECT DATE_ADD(MAX( 
                                                      TRANSACTION.paymentdates), 
                                                      interval 6 month) 
                                               FROM   TRANSACTION 
                                               WHERE  TYPE = 'REPAYMENT' 
                                                      AND TRANSACTION.accountkey 
                                                          = 
                                                          account.accountkey) 
                  GROUP  BY product.loantype) AS active 
              ON( product.loantype = active.loantype ) 
       left join (SELECT product.loantype, 
                         SUM(account.principalbalance 
                             + account.interestbalance) AS TOTALBALANCE, 
                         COUNT(1)                       AS cnt 
                  FROM   account 
                         inner join client 
                                 ON account.accountkey = client.primarykey 
                         inner join product 
                                 ON account.productkey = product.primarykey 
                  WHERE  TRANSACTION.TYPE = 'REPAYMENT' 
                         AND Current_date() < (SELECT DATE_ADD(MAX( 
                                                      TRANSACTION.paymentdates), 
                                                      interval 6 month) 
                                               FROM   TRANSACTION 
                                               WHERE  TYPE = 'REPAYMENT' 
                                                      AND TRANSACTION.accountkey 
                                                          = 
                                                          account.accountkey) 
                  GROUP  BY product.loantype) AS inactive 
              ON( product.loantype = inactive.loantype )