Pavel
Pavel

Reputation: 4410

Select minimum from maximums of groups

I have table billa which have columns: currency, amount and billed_at.

I would like to fetch minimum billed_at from maximums of billed_at of each currency

Fetching maximum billed_at for each currency can be done with this query

SELECT MAX(billed_at), currency FROM bills GROUP BY currency

For example it returned 2 rows

2013-12-16 13:31:18 UTC, "EUR"

2013-12-02 14:57:00 UTC, "USD"

Then I need to fetch minimum billed_at from this results. How can I do this?

Thanks in advance!

Upvotes: 1

Views: 65

Answers (2)

Robert
Robert

Reputation: 25753

You shuld use your query as subquery in from clause as blow

select min(billed_at)
from (SELECT MAX(billed_at), currency FROM bills GROUP BY currency) AS Tab

Upvotes: 2

Mihai
Mihai

Reputation: 26784

SELECT MIN(billed_at_max) 
FROM (SELECT MAX(billed_at)billed_at_max FROM bills GROUP BY currency)x

Upvotes: 0

Related Questions