I indicate my query immediately:
Example, I have the table 'calendar':
Código | Tramo | días
Semana1|08001800| 5
Semana1|DESCANSO| 2
Semana2|15302100| 4
Semana2|21000800| 1
Semana2|DESCANSO| 2
Semana3|09001900| 5
Semana3|DESCANSO| 2
Semana3|08001800| 2
The table indicated above is a schedule and my question is whether the result query can be obtained in the following way:
Código | Tramo | Tramo | Tramo | Tramo | Tramo | Tramo | Tramo | Tramo | Tramo |
Semana1|08001800|08001800|08001800|08001800|08001800|DESCANSO|DESCANSO|
Semana2|15302100|15302100|15302100|15302100|21000800|DESCANSO|DESCANSO|
Semana3|09001900|09001900|09001900|09001900|09001900|DESCANSO|DESCANSO|08001800|08001800|
In theory, you should read the 'TRAMO' and repeat it the number of times indicated in 'days' and you should repeat it to the right in columns and without repeating the 'code'.
At the moment I haven't advanced in the code since I can't think of how to do it, I was thinking about using CASE but it doesn't work for me.
any ideas? Is that understood? It is dynamic since the query will be extended to the side according to the number of days that the section has.
Please your guidance.
This is not the most optimal solution, I think there is a possibility to put it in a simpler way, but at least this method would solve your question.
In the first place, I base myself on your maximum limit of sections, this is important, because if this number is indeterminate, you have to go through a dynamic query that is one more layer of complexity: find out the maximum and write a statement dynamic on the basis of what I am answering you.
The other important issue is that in principle, in your example, you do not indicate a field to manage a chronological order, I am going to add it to logic with the order in which you put together the example.
The first thing is to define the table with the example cases, plus the order we were talking about:
The following is to build a table of Days per section, as you mentioned 2 weeks maximum, in the worst case we would have 1 section of 14 days, so we will generate 14 records, as follows:
We will use this table to generate another Intermediate, where basically we are going to generate a record for each day of each section, from here I tell you that it is not an optimal solution and it will be practical depending on the volume of data you handle. To build our intermediate table:
Seeing a single case, for example
Semana1
, the data would look like this:With these data it is much clearer how we can make some queries to reach the desired result:
Using a
GROUP BY
,MAX
and aCASE
we can do this (in practice we would have to add the rest of the sections up to 14):Using
PIVOT
it is also possible (I don't remember if you have it available in 2008):In both cases the output is the same:
As I was saying, it is not the most optimal solution, I suggest you wait for some other response before accepting it as good (obviously if it is useful to you).
Update
A much better solution is the following:
This solution doesn't require generating an intermediate table, sorry to boot for the most difficult solution, but I wasn't sure I could solve it any other way