Hi,
I would like to know how to amalgamate interval data using t-sql. The code below creates a temp table and I would like a query to amalgamate the intervals grouping by the field CODE,
I can use the sql
but this doesn't take into account the fact that the code value can be repeated down the interval sequence.
for example the result should be:
A1 0 2 A
A1 2 5 B
A1 5 8 C
A1 8 9 B
Thank you
I would like to know how to amalgamate interval data using t-sql. The code below creates a temp table and I would like a query to amalgamate the intervals grouping by the field CODE,
I can use the sql
Code:
select id,min(INVFROM),max(INVto),code from #temptable group by id,code
but this doesn't take into account the fact that the code value can be repeated down the interval sequence.
for example the result should be:
A1 0 2 A
A1 2 5 B
A1 5 8 C
A1 8 9 B
Thank you
Code:
DROP TABLE #TEMPTABLE
CREATE TABLE #TEMPTABLE (ID VARCHAR(10), INVFROM FLOAT, INVTO FLOAT, CODE VARCHAR(10))
INSERT INTO #TEMPTABLE VALUES('A1',0,1,'A')
INSERT INTO #TEMPTABLE VALUES('A1',1,2,'A')
INSERT INTO #TEMPTABLE VALUES('A1',2,3,'B')
INSERT INTO #TEMPTABLE VALUES('A1',3,4,'B')
INSERT INTO #TEMPTABLE VALUES('A1',4,5,'B')
INSERT INTO #TEMPTABLE VALUES('A1',5,6,'C')
INSERT INTO #TEMPTABLE VALUES('A1',6,7,'C')
INSERT INTO #TEMPTABLE VALUES('A1',7,8,'C')
INSERT INTO #TEMPTABLE VALUES('A1',8,9,'B')
SELECT ID, MIN(INVFROM), MAX(INVTO), CODE FROM #TEMPTABLE
GROUP BY ID,CODE