I'm attempting to query a table which contains a character varying[]
column of years, and return those years as a string of comma-delimited year ranges. The year ranges would be determined by sequential years present within the array, and years/year ranges which are not sequential should be separated be commas.
The reason the data-type is character varying[]
rather than integer[]
is because a few of the values contain ALL
instead of a list of years. We can omit these results.
So far I've had little luck approaching the problem as I'm not really even sure where to start.
Would someone be able to give me some guidance or provide a useful examples of how one might solve such as challenge?
years_table
Example
+=========+============================+
| id | years |
| integer | character varying[] |
+=========+============================+
| 1 | {ALL} |
| 2 | {1999,2000,2010,2011,2012} |
| 3 | {1990,1991,2007} |
+---------+----------------------------+
Output Goal:
Example SQL Query:
SELECT id, [year concat logic] AS year_ranges
FROM years_table WHERE 'ALL' NOT IN years
Result:
+====+======================+
| id | year_ranges |
+====+======================+
| 2 | 1999-2000, 2010-2012 |
| 3 | 1990-1991, 2007 |
+----+----------------------+