是否有一个 SQL 函数可以生成给定范围的序列号?

发布于 2024-10-28 12:14:23 字数 133 浏览 4 评论 0原文

我需要生成一个具有给定范围的连续整数数组,以便将其用于:

SELECT tbl.pk_id
  FROM tbl
 WHERE tbl.pk_id NOT IN (sequential array);

I need to generate an array of sequential integers with a given range in order to use it in:

SELECT tbl.pk_id
  FROM tbl
 WHERE tbl.pk_id NOT IN (sequential array);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

穿越时光隧道 2024-11-04 12:14:24

如果您有给定的连续整数范围(即起点和终点),您应该能够使用 BETWEEN 关键字:

SELECT tbl.pk_id
  FROM tbl
 WHERE tbl.pk_id NOT BETWEEN START_INT AND END_INT

还是我误解了您的问题..?

If you have a given range - ie a start point and an end point - of sequential integers you should just be able to use the BETWEEN keyword:

SELECT tbl.pk_id
  FROM tbl
 WHERE tbl.pk_id NOT BETWEEN START_INT AND END_INT

or am I misunderstanding your question..?

土豪 2024-11-04 12:14:24

因为你说你已经有了一个数字表,所以我建议这样做:

SELECT element
FROM series
WHERE element NOT IN (SELECT pk_id
                      FROM tbl)

可能比你尝试过的查询更有效。

Because you say you've already got a number table, I would suggest this:

SELECT element
FROM series
WHERE element NOT IN (SELECT pk_id
                      FROM tbl)

Might possibly be more efficient than the query you've tried.

幸福%小乖 2024-11-04 12:14:24

两个想法。 。 。

首先,没有标准的 SQL 函数可以做到这一点。但有些系统包含确实生成序列的非标准函数。例如,在 PostgreSQL 中,您可以使用generate_series() 函数。

select generate_series(1,100000);
1
2
3
...
100000

该函数本质上返回一个表;它可以在连接中使用。

如果 Informix 没有执行类似操作的函数,也许您可​​以编写一个执行类似操作的 Informix SPL 函数。

其次,您可以创建一个单列表并用一系列整数填充它。这适用于所有平台,并且不需要编程。它只需要最少的维护。 (您需要在此表中保留比在生产表中使用的整数更多的整数。)

create table integers (
    i integer primary key
);

使用电子表格或实用程序生成一系列整数来填充它。如果您有 Unix、Linux 或 Cygwin 环境,最简单的方法是使用 seq

$ seq 1 5 > integers
$ cat integers
1
2
3
4
5

Informix 有一个免费开发者版本供您下载。也许你可以用它构建一个引人注目的演示,并且管理层会让你升级。

Two thoughts . . .

First, there's no standard SQL function that does that. But some systems include a non-standard function that does generates a series. In PostgreSQL, for example, you can use the generate_series() function.

select generate_series(1,100000);
1
2
3
...
100000

That function essentially returns a table; it can be used in joins.

If Informix doesn't have a function that does something similar, maybe you can write an Informix SPL function that does.

Second, you could just create a one-column table and populate it with a series of integers. This works on all platforms, and doesn't require programming. It requires only minimal maintenance. (You need to keep more integers in this table than you're using in your production table.)

create table integers (
    i integer primary key
);

Use a spreadsheet or a utility program to generate a series of integers to populate it. The easiest way if you have a Unix, Linux, or Cygwin environment laying around is to use seq.

$ seq 1 5 > integers
$ cat integers
1
2
3
4
5

Informix has a free developer version you can download. Maybe you can build a compelling demo with it, and management will let you upgrade.

や莫失莫忘 2024-11-04 12:14:24

我将建议一个通用的解决方案来创建一个包含给定 k 的正整数 0 .. 2^k-1 的结果集,以便随后用作子查询、视图或物化视图。
下面的代码说明了 k=2 的技术。

SELECT bv0 + 2* bv1 + 4*bv2   val
  FROM (
        SELECT *
          FROM 
                             (
                                SELECT 0 bv0 FROM DUAL
                                 UNION
                                SELECT 1 bv0 FROM DUAL
                             ) bit0
                 CROSS JOIN  (
                                SELECT 0 bv1 FROM DUAL
                                 UNION
                                SELECT 1 bv1 FROM DUAL
                             ) bit1
                 CROSS JOIN  (
                                SELECT 0 bv2 FROM DUAL
                                 UNION
                                SELECT 1 bv2 FROM DUAL
                             ) bit2
      ) pow2
;

我希望这对您的任务有所帮助,

最诚挚的问候,

卡斯滕

i'll suggest a generic solution to create a result set containing the positive integers 0 .. 2^k-1 for a given k for subsequent use as a subquery, view or materialized view.
the code below illustrates the technique for k=2.

SELECT bv0 + 2* bv1 + 4*bv2   val
  FROM (
        SELECT *
          FROM 
                             (
                                SELECT 0 bv0 FROM DUAL
                                 UNION
                                SELECT 1 bv0 FROM DUAL
                             ) bit0
                 CROSS JOIN  (
                                SELECT 0 bv1 FROM DUAL
                                 UNION
                                SELECT 1 bv1 FROM DUAL
                             ) bit1
                 CROSS JOIN  (
                                SELECT 0 bv2 FROM DUAL
                                 UNION
                                SELECT 1 bv2 FROM DUAL
                             ) bit2
      ) pow2
;

i hope that helps you with your task

best regards,

carsten

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文