MySQL在一个语句之间使用max(date)

问题描述 投票:0回答:1

任何帮助表示赞赏。我试图在CASE语句中使用max(date),我无法正确获取语法或??

// This works    
SELECT
     ROUND(sum(CASE WHEN Document_Date BETWEEN  DATE_FORMAT("2019-01-01", "%Y-%m-%d") AND  DATE_FORMAT("2019-01-31", "%Y-%m-%d") THEN GTotal END),0) TOTAL2019
FROM
salesdata

// But I want to use max(Document_Date) which is 2019-01-31 like so and have tried cast and different combos, but it comes out null or says invalid use of group function.  Document_Date is datetime in DB

    SELECT
         ROUND(sum(CASE WHEN Document_Date BETWEEN  DATE_FORMAT("2019-01-01", "%Y-%m-%d") AND  DATE_FORMAT(max(Document_Date), "%Y-%m-%d") THEN GTotal END),0) TOTAL2019
    FROM
    salesdata

我做错了什么 - 谢谢你的帮助。

mysql max case between
1个回答
0
投票

你正试图在qazxsw poi中使用qazxsw poi aggregate。通常,如果不使用子查询,则无法执行此操作。

检查我的替代方案

max
© www.soinside.com 2019 - 2024. All rights reserved.