dbTalk Databases Forums  

Retrieve next lowest number in mixed set

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Retrieve next lowest number in mixed set in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Zamdrist
 
Posts: n/a

Default Retrieve next lowest number in mixed set - 10-04-2007 , 02:17 PM






Let's say I have a result set (records) containing numbers such as:

0
1
2
3
9
16
21
45
100
101
102
103
104
105
106

How might someone write a procedure to get the next lowest number in
this sequence? In this case it would be: 4. Some combination of
Select, Min & When, I am sure, but it escapes me at the moment.

Thoughts? Thanks...


Reply With Quote
  #2  
Old   
David Portas
 
Posts: n/a

Default Re: Retrieve next lowest number in mixed set - 10-04-2007 , 03:31 PM






"Zamdrist" <zamdrist (AT) gmail (DOT) com> wrote

Quote:
Let's say I have a result set (records) containing numbers such as:

0
1
2
3
9
16
21
45
100
101
102
103
104
105
106

How might someone write a procedure to get the next lowest number in
this sequence? In this case it would be: 4. Some combination of
Select, Min & When, I am sure, but it escapes me at the moment.

Thoughts? Thanks...

SELECT MIN(x)+1 x
FROM tbl t
WHERE NOT EXISTS
(SELECT x
FROM tbl
WHERE x = t.x+1);

--
David Portas




Reply With Quote
  #3  
Old   
Zamdrist
 
Posts: n/a

Default Re: Retrieve next lowest number in mixed set - 10-04-2007 , 04:10 PM



On Oct 4, 3:31 pm, "David Portas"
<REMOVE_BEFORE_REPLYING_dpor... (AT) acm (DOT) org> wrote:
Quote:
"Zamdrist" <zamdr... (AT) gmail (DOT) com> wrote in message

news:1191525448.343516.292440 (AT) k79g2000hse (DOT) googlegroups.com...



Let's say I have a result set (records) containing numbers such as:

0
1
2
3
9
16
21
45
100
101
102
103
104
105
106

How might someone write a procedure to get the next lowest number in
this sequence? In this case it would be: 4. Some combination of
Select, Min & When, I am sure, but it escapes me at the moment.

Thoughts? Thanks...

SELECT MIN(x)+1 x
FROM tbl t
WHERE NOT EXISTS
(SELECT x
FROM tbl
WHERE x = t.x+1);

--
David Portas
Thanks!



Reply With Quote
  #4  
Old   
David Portas
 
Posts: n/a

Default Re: Retrieve next lowest number in mixed set - 10-04-2007 , 04:46 PM



"Zamdrist" <zamdrist (AT) gmail (DOT) com> wrote

Quote:
Let's say I have a result set (records) containing numbers such as:

0
1
2
3
9
16
21
45
100
101
102
103
104
105
106

How might someone write a procedure to get the next lowest number in
this sequence? In this case it would be: 4. Some combination of
Select, Min & When, I am sure, but it escapes me at the moment.

Thoughts? Thanks...

Another method (SQL Server 2005 only):

SELECT MIN(x) x
FROM
(SELECT x+1 FROM tbl
EXCEPT
SELECT x FROM tbl) t(x);

--
David Portas




Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Retrieve next lowest number in mixed set - 10-04-2007 , 05:02 PM



Zamdrist (zamdrist (AT) gmail (DOT) com) writes:
Quote:
Let's say I have a result set (records) containing numbers such as:

0
1
2
3
9
16
21
45
100
101
102
103
104
105
106

How might someone write a procedure to get the next lowest number in
this sequence? In this case it would be: 4. Some combination of
Select, Min & When, I am sure, but it escapes me at the moment.
Here's another that requires SQL 2005:

SELECT MIN(rn - 1 + minacsid)
FROM (SELECT acsid, rn = row_number() Over(ORDER BY acsid),
minacsid = MIN(acsid) OVER()
FROM accountstats) AS x
WHERE rn - 1 + minacsid <> acsid



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #6  
Old   
--CELKO--
 
Posts: n/a

Default Re: Retrieve next lowest number in mixed set - 10-04-2007 , 10:36 PM



Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. If you know how, follow ISO-11179 data element naming
conventions and formatting rules.

Quote:
How might someone write a procedure to get the next lowest number in this sequence? In this case it would be: 4.
No; read your own specs! The answer is -1. Or maybe -0.0000...1 if
the column is a DECIMAL or a FLOAT. If you had posted DDL that
limited the column to non-negative integers, then Dave's answer would
work.



Reply With Quote
  #7  
Old   
strawberry
 
Posts: n/a

Default Re: Retrieve next lowest number in mixed set - 10-06-2007 , 04:29 PM



On 6 Oct, 18:34, Rodrigo De León <rdele... (AT) gmail (DOT) com> wrote:
Quote:
On Oct 4, 2:17 pm, Zamdrist <zamdr... (AT) gmail (DOT) com> wrote:

In this case it would be: 4.

SELECT MIN(t2.x +1)
FROM t FULL JOIN t t2 ON t.x = t2.x +1
WHERE t.x IS NULL;
How about:

SELECT t1.sequence_id +1 vacancy
FROM sequence t1
LEFT JOIN sequence t2 ON t1.sequence_id +1 = t2.sequence_id
WHERE t2.sequence_id IS NULL
ORDER BY vacancy
LIMIT 1



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.