I have sql view like this:
id date_from date_to
1 2005-01-05 2005-05-10
1 2005-01-05 2005-05-10
1 2005-01-05 2005-05-10
2 2005-01-01 2006-08-31
2 2010-01-01 2010-06-30
3 2005-01-01 2005-06-30
And I want to write sql statement which returns:
1 2005-01-05 2005-05-10 2005-01-05 2005-05-10 2005-01-05 2005-05-10
2 2005-01-01 2006-08-31 2010-01-01 2010-06-30 NULL NULL
3 2005-01-01 2005-06-30 NULL NULL NULL NULL
Any ideas?
Answers to Mark's question will help.
using t-sql, I explicitly handle 3 levels. If you want it to be dynamic, you need to create the query dynamically.
Test @ http://sqlfiddle.com/#!3/9daae/4/0
Because you're most recent questions have been related to MySQL, I'll assume you want a MySQL solution.
If you know the max number of potential date ranges, then you can use MAX and CASE. However, you have to have a row counter since you don't have any other unique identifier (I'd actually recommend adding that to your view since you mention this is a view). Here it is though:
If you don't know the maximum number of date ranges, then you won't be able to do this with a single SQL statement. Instead, you'll need to use dynamic SQL.
I'll assume you can add the row_number to your View and here is a close example:
Best of luck.