在oracle中为多个ranges in between子句运行一个查询

cgfeq70w  于 2021-07-24  发布在  Java
关注(0)|答案(5)|浏览(280)

我在oracle中使用下面的sql查询来获得一个范围内的行数

SELECT count(SERIALNR)
  FROM my_tab
 WHERE SERIALNR BETWEEN '93266070760000' AND '93266070809999';

但是,如果我必须对多个范围运行一个查询(如下面的all-in-one查询),该如何执行查询呢

93266070560000  93266070609999
93266070610000  93266070659999
93266070660000  93266070709999
93266070710000  93266070759999
93266070760000  93266070809999

注:类型 SERIALNRNUMBER

gcuhipw9

gcuhipw91#

你好像需要 UNION ALL 分别获取每个范围

SELECT 'Range 1' AS "Range", count (SERIALNR) AS "Count"
  FROM tab
 WHERE SERIALNR BETWEEN 93266070560000 AND 93266070809999
 UNION ALL 
SELECT 'Range 2', count (SERIALNR)
  FROM tab
 WHERE SERIALNR BETWEEN 93266070610000 AND 93266070659999
 UNION ALL 
SELECT 'Range 3', count (SERIALNR)
  FROM tab
 WHERE SERIALNR BETWEEN 93266070660000 AND 93266070709999
 UNION ALL 
SELECT 'Range 4', count (SERIALNR)
  FROM tab
 WHERE SERIALNR BETWEEN 93266070710000 AND 93266070759999
 UNION ALL 
SELECT 'Range 5', count (SERIALNR)
  FROM tab
 WHERE SERIALNR BETWEEN 93266070760000 AND 93266070809999

你不需要引用 SERIALNR 具有数字数据类型。

5n0oy7gb

5n0oy7gb2#

多个或多个条件呢-

SELECT  count (SERIALNR)
FROM my_tab
WHERE (SERIALNR BETWEEN '93266070760000' AND   '93266070809999'
       OR SERIALNR BETWEEN '93266070560000' AND   '93266070609999'
       OR SERIALNR BETWEEN '93266070610000' AND   '93266070659999'
       OR SERIALNR BETWEEN '93266070660000' AND   '93266070709999'
       OR SERIALNR BETWEEN '93266070710000' AND   '93266070759999'
       OR SERIALNR BETWEEN '93266070760000' AND   '93266070809999');
nnt7mjpx

nnt7mjpx3#

你有用处 OR 条款,

SELECT  count (SERIALNR) FROM my_tab WHERE 
(SERIALNR  BETWEEN '93266070760000' AND   '93266070809999')
OR
(SERIALNR  BETWEEN '93266070610000' AND   '93266070659999')
OR
(SERIALNR  BETWEEN '93266070660000' AND   '93266070709999')
OR
(SERIALNR  BETWEEN '93266070710000' AND   '93266070759999')
OR
(SERIALNR  BETWEEN '93266070760000' AND   '93266070809999');

如果将这些值存储在任何其他表中,会更方便

idv4meu8

idv4meu84#

我想你需要在每个语句之间使用大括号 OR 就像下面一样

( 
  SERIALNR BETWEEN '93266070760000' AND   '93266070809999'
  OR
..... other ranges comes here
) and
OTHER cONDITIONS

您可以查看有关逻辑条件的文档

g52tjvyc

g52tjvyc5#

如果需要列中的计数,则需要条件聚合:

SELECT 
  COUNT(CASE WHEN serialnr BETWEEN '93266070760000' AND '93266070809999' THEN 1 END) AS c1,
  COUNT(CASE WHEN serialnr BETWEEN '93266070610000' AND '93266070659999' THEN 1 END) AS c2,
  COUNT(CASE WHEN serialnr BETWEEN '93266070660000' AND '93266070709999' THEN 1 END) AS c3,
  COUNT(CASE WHEN serialnr BETWEEN '93266070710000' AND '93266070759999' THEN 1 END) AS c4,
  COUNT(CASE WHEN serialnr BETWEEN '93266070760000' AND '93266070809999' THEN 1 END) AS c5
FROM my_tab 
WHERE serialnr BETWEEN '93266070560000' AND '93266070809999';

或为 UNION ALL ,如果要按行计数:

WITH ranges AS
(
  SELECT '93266070760000' AS s1, '93266070809999' AS s2 FROM DUAL
  UNION ALL
  SELECT '93266070610000' AS s1, '93266070659999' AS s2 FROM DUAL
  UNION ALL
  SELECT '93266070660000' AS s1, '93266070709999' AS s2 FROM DUAL
  UNION ALL
  SELECT '93266070710000' AS s1, '93266070759999' AS s2 FROM DUAL
  UNION ALL
  SELECT '93266070760000' AS s1, '93266070809999' AS s2 FROM DUAL
)
SELECT
  s1,
  s2,
  (SELECT COUNT(*) FROM my_tab WHERE serialnr BETWEEN r.s1 AND r.s2) AS cnt
FROM ranges r
ORDER BY s1;

相关问题