简体   繁体   English

MySQL查询最小和最大分组

[英]MySql query for min and max with grouping

Input Table : 输入表:

表

By considering the above table I want to list the start and end date grouped by order id for the given input status "b" . 通过考虑上表,我想针对给定的输入状态“ b” 列出按订单ID分组的开始和结束日期 Right now I am doing it with many sql queries and merging them together in java. 现在,我正在使用许多sql查询并在Java中将它们合并在一起。 But I would like to write it in single sql query in mysql. 但是我想在mysql中的单个sql查询中编写它。 Can anyone help me to write this in single sql query. 谁能帮助我在单个sql查询中编写此代码。

Output : 输出:

在此处输入图片说明

Use this: 用这个:

SELECT OrderId, MIN(createdDate) as MinDate, MAX(createdDate) as MaxDate
FROM tbl1
WHERE fromStatus = 'b' or inputStatus = 'b'
GROUP BY OrderId

I suspect you lack the WHERE clause in your query. 我怀疑您的查询中缺少WHERE子句。

SELECT  orderID, 
        MIN(createdDate) min_date,
        MAX(createdDate) max_date
FROM    tableName
WHERE   'b' IN (fromStatus, toStatus)
GROUP   BY OrderID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM