Retrieve next lowest number in mixed set
Retrieve next lowest number in mixed set
am 04.10.2007 21:17:28 von zamdrist
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...
Re: Retrieve next lowest number in mixed set
am 04.10.2007 22:31:19 von David Portas
"Zamdrist" wrote in message
news:1191525448.343516.292440@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
Re: Retrieve next lowest number in mixed set
am 04.10.2007 23:10:00 von zamdrist
On Oct 4, 3:31 pm, "David Portas"
wrote:
> "Zamdrist" wrote in message
>
> news:1191525448.343516.292440@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!
Re: Retrieve next lowest number in mixed set
am 04.10.2007 23:46:32 von David Portas
"Zamdrist" wrote in message
news:1191525448.343516.292440@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
Re: Retrieve next lowest number in mixed set
am 05.10.2007 00:02:56 von Erland Sommarskog
Zamdrist (zamdrist@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@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
Re: Retrieve next lowest number in mixed set
am 05.10.2007 05:36:37 von Joe Celko
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.
Re: Retrieve next lowest number in mixed set
am 06.10.2007 19:34:45 von Rodrigo De Leon
On Oct 4, 2:17 pm, Zamdrist 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;
Re: Retrieve next lowest number in mixed set
am 06.10.2007 23:29:44 von zac.carey
On 6 Oct, 18:34, Rodrigo De Le=F3n wrote:
> On Oct 4, 2:17 pm, Zamdrist 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