I have a table with a field called sector, each sector is usually something like 1,2,3,4,5,6,7,etc.
I want to show available sectors in an application, I thought that showing all 1,2,3,4,5,6,7 is dumb so I should show “1 to 7” instead.
The problem is that sometimes the sectors skip one number like this 1,2,3, 5,6,7.
So I want to show something like 1 to 3, 5 to 7.
how could I query this in sql to show in my app?
Some DBMS might have some OLAP functionality that makes it easy to write such queries, but IBM Informix Dynamic Server (IDS) does not yet have such functions.
Let’s assume, for sake of concreteness, that your table is called ‘ProductSectors’ and has a structure like:
What you are seeking within a particular ProductID is a list of the minimum and maximum contiguous values of Sector. A range is contiguous when there is no value one smaller than the minimum and no value one bigger than the maximum and there is no gap within the range. This is a complex query:
And here is a trace of the overall query working with sample data:
And some sample data, with various gaps:
Required output – also the actual output:
With the expected results on MacOS X 10.6.2, IDS 11.50.FC4W1, SQLCMD 86.04.