dbTalk Databases Forums  

Retrieve next lowest number in mixed set

comp.databases comp.databases


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



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

Default Retrieve next lowest number in mixed set - 10-04-2007 , 03: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 , 04: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 , 05: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 , 05: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 , 06: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 , 11: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 , 05: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
  #8  
Old   
Zamdrist
 
Posts: n/a

Default RE: Retrieve next lowest number in mixed set - 12-10-2007 , 01:52 PM



I missed this, forgive the dated response.

CELKO...explain to me then why everyone else in the thread knew
*exactly* what was asked for? Are you predisposed to being a dickhead,
or is it just me?

On Oct 4, 9:36 pm, --CELKO-- <jcelko... (AT) earthlink (DOT) net> wrote:
Quote:
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.

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
  #9  
Old   
--CELKO--
 
Posts: n/a

Default Re: Retrieve next lowest number in mixed set - 12-11-2007 , 09:15 AM



Quote:
CELKO...explain to me then why everyone else in the thread knew *exactly* what was asked for? Are you predisposed to being a dickhead, or is it just me?
It's you
One of the hardest things for newbies to learn is how to write a clear
spec. Anohter one is not to use profanity in newsgroups ..


Reply With Quote
  #10  
Old   
Gene Wirchenko
 
Posts: n/a

Default Re: Retrieve next lowest number in mixed set - 12-11-2007 , 02:33 PM



--CELKO-- <jcelko212 (AT) earthlink (DOT) net> wrote:

Quote:
CELKO...explain to me then why everyone else in the thread knew *exactly* what was asked for? Are you predisposed to being a dickhead, or is it just me?
Or thought they did and maybe even guessed right. From the
original post, I do not know what is wanted. Next lowest from what?

Quote:
It's you
One of the hardest things for newbies to learn is how to write a clear
spec. Anohter one is not to use profanity in newsgroups ..
OP should have a read of Eric Steven Raymond's "How To Ask
Questions The Smart Way"
(<http://www.catb.org/~esr/faqs/smart-questions.html>).

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 - 2008, Jelsoft Enterprises Ltd.