Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
tpa32
Starting Member
3 Posts |
Posted - 2010-10-05 : 16:46:57
|
| Hi All,I need to write a function which will return me a range of integers assigned to some unique ID.my sample data is as follow.TableID UniqueID No93986 10777 28393987 10777 28493988 10777 28593989 10777 28693990 10777 28793991 10777 28893992 10777 28993993 10777 29093994 10777 29193995 10777 29293996 10777 135393997 10777 135493998 10777 135593999 10777 135694000 10777 135794001 10777 135894002 10777 135994003 10777 136094004 10777 136194005 10777 1362I would expect the return result for 10777 uniqueID would be283-292,1353-1362if data is as follows:-TableID UniqueID No93986 10777 28393988 10777 28593989 10777 28693990 10777 28793991 10777 28893992 10777 28993993 10777 29093994 10777 29193995 10777 29293996 10777 135393997 10777 135493999 10777 135694000 10777 135794001 10777 135894002 10777 135994003 10777 136094004 10777 136194005 10777 1362then result would be like283,285-292,1353-1354,1356-1362and if data is as follow93996 10777 135393997 10777 135493999 10777 135694000 10777 135794001 10777 135894002 10777 135994003 10777 136094004 10777 136194005 10777 1362then range would be something like 1353-1362Thanks |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-06 : 02:15:51
|
| [code]declare @mntbl as table(id int)insert into @mntblselect 1 union allselect 2 union allselect 3 union allselect 4 union allselect 8 union allselect 9 union allselect 11select convert(varchar(20),min(id)) + '-' + convert(varchar(20),max(id))range from(select *,id-row_number()over(order by id)rid from @mntbl )t group by rid[/code]PBUH |
 |
|
|
kunal.mehta
Yak Posting Veteran
83 Posts |
Posted - 2010-10-06 : 02:36:10
|
| enhance this to get yr resultsdeclare @Maintable as table( TableID int, uniqueID int, number int)insert into @Maintableselect 93986, 10777 ,283union select 93987, 10777 ,284union select 93988, 10777 ,285union select 93989, 10777 ,286union select 93990, 10777 ,287union select 93991, 10777 ,288union select 93992, 10777 ,289 union select 93993, 10777 ,290 union select 93994 ,10777 ,291 union select 93995, 10777 ,292 union select 93996 ,10777 ,1353 union select 93997 ,10777 ,1354 union select 93998 ,10777 ,1355 union select 93999 ,10777 ,1356 union select 94000 ,10777 ,1357 union select 94001 ,10777 ,1358 union select 94002 ,10777 ,1359 union select 94003 ,10777 ,1360 union select 94004 ,10777 ,1361 union select 94005 ,10777, 1362 ;with cte as(select *,ROW_NUMBER() over (order by number asc) localindex from @Maintable),cte2 as(select number,number - isnull((select number from cte s where s.localindex=b.localindex-1),0) diff,localindex from cte b),cte3 as(select * from cte2 awhere diff <> 1 or isnull((select DIFF from cte2 b where b.localindex = a.localindex+1),0) <>1)select * from cte3 order by localindex asc |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-06 : 02:54:37
|
Kunal thx for the sample dataselect convert(varchar(20),min(number)) + '-' + convert(varchar(20),max(number))range from( select *,number-row_number()over(order by number)rid from @Maintable )t group by rido/prange-----------------------------------------283-2921353-1362 PBUH |
 |
|
|
kunal.mehta
Yak Posting Veteran
83 Posts |
Posted - 2010-10-06 : 03:16:09
|
| gd one |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-06 : 03:29:19
|
quote: Originally posted by kunal.mehta gd one
ThanksPBUH |
 |
|
|
tpa32
Starting Member
3 Posts |
Posted - 2010-10-06 : 10:37:13
|
| Thanks you all for your reply.I rather generated a string in one column from multiple rows.and I will handle the range in my c# code.I used this code to get me a string.Select Main.SubjectID, Left(Main.Students,Len(Main.Students)-1) As "Students"From(Select distinct ST2.SubjectID, (Select ST1.StudentName + ',' AS [text()] From dbo.Students ST1 Where ST1.SubjectID = ST2.SubjectID ORDER BY ST1.SubjectID For XML PATH ('')) [Students] From dbo.Students ST2) [Main] |
 |
|
|
|
|
|
|
|