2

I have googled but did not find anything related. I have a MySQL table like this:

+++++++++++++++++++++++++++++++
| roomID |    date    | price |
+++++++++++++++++++++++++++++++
|  1     | 2012-10-10 |   10  |
|  1     | 2012-10-11 |   10  |
|  1     | 2012-10-12 |   10  |
|  1     | 2012-10-13 |   12  |
|  2     | 2012-10-10 |   15  |
|  2     | 2012-10-11 |   15  |
|  2     | 2012-10-12 |   15  |
|  2     | 2012-10-13 |   16  |
|  2     | 2012-10-14 |   16  |
|  2     | 2012-10-15 |   16  |
+++++++++++++++++++++++++++++++

I need to get periods based on price and roomID:

++++++++++++++++++++++++++++++++++++++++++++
| roomID |    from    |    till    | price |
++++++++++++++++++++++++++++++++++++++++++++
|    1   | 2012-10-10 | 2012-10-12 |   10  |
|    1   | 2012-10-13 | 2012-10-13 |   12  |
|    2   | 2012-10-10 | 2012-10-12 |   15  |
|    2   | 2012-10-13 | 2012-10-15 |   16  |
++++++++++++++++++++++++++++++++++++++++++++

Thank you!

2 Answers 2

6
select roomid,
       min(date) as from,
       max(date) as till,
       price
from periods
group by price
order by price
Sign up to request clarification or add additional context in comments.

Comments

4

You can try using the following query:

SELECT roomid, MIN(date) AS `from`, MAX(date) AS `till`, price
FROM tableName
GROUP BY price
ORDER BY price

1 Comment

+1 for using backticks ( ` ) over key words as column/expression names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.