DonX
DonX

Reputation: 16379

MySql query for min and max with grouping

Input Table :

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 :

enter image description here

Upvotes: 0

Views: 94

Answers (2)

John Woo
John Woo

Reputation: 263943

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

Upvotes: 2

Iswanto San
Iswanto San

Reputation: 18569

Use this:

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

Upvotes: 2

Related Questions