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 |
|
vickycao
Starting Member
2 Posts |
Posted - 2004-06-01 : 23:09:44
|
| dear all :I want select the records within 10for example:ID N1---------------------0006 50006 200006 210006 22the Results is:0006 50006 20because 20-5>10 so select 0006 5because 20,21,22 interval <10 so select only one record 0006 20Any advice, links or pointers would be greatly appreciated.Thanks a lot.Vicky Cao |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-06-01 : 23:36:20
|
| This is rough, but:DECLARE @test TABLE(ID CHAR(4), N1 INT)INSERT @test(ID, N1) SELECT 0006,5 UNION ALL SELECT 0006,20 UNION ALL SELECT 0006,21 UNION ALL SELECT 0006,22DECLARE @searchnumber INT, @interval INTSELECT @searchnumber = 20, @interval = 10SELECT ID, N1FROM @testWHERE N1 BETWEEN @searchnumber AND (@searchnumber-(@interval+1)) OR N1 BETWEEN @searchnumber AND (@searchnumber+(@interval-1))MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
vickycao
Starting Member
2 Posts |
Posted - 2004-06-02 : 00:19:44
|
| thank you for your reply.but why need @searchnumber param ?if use cursor can solve this problem.means current record N1 - previous Record N1 >10 then select currentelse ignore .but how can I do this without use cursor.thanks a lot .vickycao |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2004-06-02 : 07:10:37
|
| Have a look at the following....http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=17768&SearchTerms=sequenceyou may be able to adapt it to spot gaps....which match your target criteria.One issue you have to recognise is that "logically" you must treat the input set as having NO ORDER on the data....unless you EXPLICITLY put one on it...It is entirely valid for the SQL engine to process the input data set as IF it was in the following sequenceID N1---------------------0006 50006 210006 200006 22and then as ID N1---------------------0006 50006 220006 200006 21in 2 consecutative runs...(which will affect your output/result set)...UNLESS you EXPLICITY ORDER the input data.....This arguement/point has been explored here many times. |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-06-02 : 08:50:06
|
| I'm going to go beat my head against a wall.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
|
|
|
|
|