如何从多个返回值中找到MAX

时间:2012-11-24 18:31:51

标签: mysql

下面是我的sql语句,一切正常,我不知道如何从多个返回值中找到MAX。无论如何这里是我的代码,我有问题的部分标有[]。

SELECT 
(product1.CNT+product2.CNT+product3.CNT)AS CNT,
(
 IFNULL(product1.SUMAMT,0)+
 IFNULL(product2.SUMAMT,0)+
 IFNULL(product3.SUMAMT,0)
)AS AMTAMT,

[MAX(product1.MXDT,product2.MXDT,product3.MXDT)AS MAXDT]

FROM 

(SELECT 
 SUM(LAMT)AS SUMAMT, 
 COUNT(LAMT)AS CNT,
 IFNULL(LSUBDT,(CURDATE()-INTERVAL 9999 DAY))AS MXDT
 FROM product1 WHERE LSUBBY=1 AND RECST=1
)product1 

CROSS JOIN 
(SELECT 
 SUM(LAMT)AS SUMAMT, 
 COUNT(LAMT)AS CNT,
 IFNULL(LSUBDT,(CURDATE()-INTERVAL 9999 DAY))AS MXDT
 FROM product2 WHERE LSUBBY=1 AND RECST=1
)product2

CROSS JOIN 
(SELECT 
 SUM(LAMT)AS SUMAMT, 
 COUNT(LAMT)AS CNT,
 IFNULL(LSUBDT,(CURDATE()-INTERVAL 9999 DAY))AS MXDT
 FROM product3 WHERE LSUBBY=1 AND RECST=1
)product3

1 个答案:

答案 0 :(得分:1)

您正在寻找的功能称为GREATEST

...
GREATEST(product1.MXDT, product2.MXDT, product3.MXDT) AS MAXDT
....