Retrieve next lowest number in mixed set

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...
zamdrist [ Do, 04 Oktober 2007 21:17 ] [ ID #1836091 ]

Re: Retrieve next lowest number in mixed set

"Zamdrist" <zamdrist [at] gmail.com> wrote in message
news:1191525448.343516.292440 [at] k79g2000hse.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
David Portas [ Do, 04 Oktober 2007 22:31 ] [ ID #1836092 ]

Re: Retrieve next lowest number in mixed set

On Oct 4, 3:31 pm, "David Portas"
<REMOVE_BEFORE_REPLYING_dpor... [at] acm.org> wrote:
> "Zamdrist" <zamdr... [at] gmail.com> wrote in message
>
> news:1191525448.343516.292440 [at] k79g2000hse.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!
zamdrist [ Do, 04 Oktober 2007 23:10 ] [ ID #1836093 ]

Re: Retrieve next lowest number in mixed set

"Zamdrist" <zamdrist [at] gmail.com> wrote in message
news:1191525448.343516.292440 [at] k79g2000hse.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...
>

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
David Portas [ Do, 04 Oktober 2007 23:46 ] [ ID #1836094 ]

Re: Retrieve next lowest number in mixed set

Zamdrist (zamdrist [at] gmail.com) writes:
> 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.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx
Erland Sommarskog [ Fr, 05 Oktober 2007 00:02 ] [ ID #1836095 ]

Re: Retrieve next lowest number in mixed set

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.
Joe Celko [ Fr, 05 Oktober 2007 05:36 ] [ ID #1837337 ]

Re: Retrieve next lowest number in mixed set

On Oct 4, 2:17 pm, Zamdrist <zamdr... [at] gmail.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;
Rodrigo De Leon [ Sa, 06 Oktober 2007 19:34 ] [ ID #1838068 ]

Re: Retrieve next lowest number in mixed set

On 6 Oct, 18:34, Rodrigo De Le=F3n <rdele... [at] gmail.com> wrote:
> On Oct 4, 2:17 pm, Zamdrist <zamdr... [at] gmail.com> wrote:
>
> > In this case it would be: 4.
>
> SELECT MIN(t2.x +1)
> FROM t FULL JOIN t t2 ON t.x =3D 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 =3D t2.sequence_id
WHERE t2.sequence_id IS NULL
ORDER BY vacancy
LIMIT 1
zac.carey [ Sa, 06 Oktober 2007 23:29 ] [ ID #1838071 ]
Datenbanken » comp.databases.ms-sqlserver » Retrieve next lowest number in mixed set

Vorheriges Thema: Full Text Index
Nächstes Thema: I want to parse @ArrayOfDays into @d1 through @d5