简体   繁体   中英

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" . Right now I am doing it with many sql queries and merging them together in java. But I would like to write it in single sql query in mysql. Can anyone help me to write this in single sql query.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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