My table looks like this:
from | to | status |
---|---|---|
1 | 3 | invalid |
10 | 15 | valid |
How can I efficiently SELECT
or produce a result like this:
serial | status |
---|---|
1 | invalid |
2 | invalid |
3 | invalid |
10 | valid |
11 | valid |
12 | valid |
13 | valid |
14 | valid |
15 | valid |
5
Answers
Using a calendar table approach we can try:
Demo
Note in the event that your actual data could have from-to ranges which might overlap, you may want to consider adding a primary key to your table. This primary key could then be used in ordering the output as you want to see it.
A generalized solution would require a recursive query:
Check the demo here.
Use
lateral join
.first CTE is your table,so:
produces
Except for the column that is named
from
(reserved keyword), the query is simple: