Oracle SQL在两个YEAR和MONTH之间选择

3lxsmp7m  于 2023-05-16  发布在  Oracle
关注(0)|答案(3)|浏览(146)

我有一个四列的SQL表。我试图在过去的一年和一个月之间得到行。以下查询返回零行。这是正确的,还是应该使用BETWEEN。列数据类型为NUMBER AND而不是DATE。以下查询返回零条记录。

select count( *) from TABLEA where refyear= 2020 AND refmonth in (1,2,3,4,5) AND 
refyear= 2019 AND refmonth in (1,2,3,4,5,6,7,8)

TABLEA
YEAR MONTH USERID NAME
2020   1    1      ABC
2020   2    1      ABC
2020   3    1      ABC
2020   4    1      ABC
2020   5    1      ABC2020
2019   1     1     ABC
2019   2     1     ABC
2019   3     1     ABC
2019   4     1     ABC
2019   5     1     ABC
2019   6     1     ABC
2019   7     1     ABC
2019   8     1     ABC
gg58donl

gg58donl1#

我认为正常的情况是:

where (refyear = 2020 AND refmonth in (1,2,3,4,5)) or
(refyear= 2019 AND refmonth in (1,2,3,4,5,6,7,8))

但我还是不确定这是不是你真正想要的。按照你写逻辑条件的方式,没有行是正常的,因为你可能不会同时有refyear = 2020和refyear= 2019。

zzlelutf

zzlelutf2#

您的查询不正确,因为您试图获取同时具有refyear值的行,因为20192020是不可能的,这就是为什么您没有得到任何行。如果你想得到一个或另一个,那么你需要使用OR而不是AND
如果您试图获取2019年8月至2020年5月之间的所有行,则:

SELECT count(*)
FROM   TABLEA
WHERE  ( refyear = 2020 AND refmonth <= 5 )
OR     ( 2019 < refyear AND refyear < 2020 )
OR     ( refyear = 2019 AND refmonth >= 8 )
  • 注意:在这种情况下,OR ( 2019 < refyear AND refyear < 2020 )永远不会为真,因此可以省略该行,但在开始和结束年份不连续的一般情况下,则需要它。

如果你想要列出所有的年份和月份(而不是连续的范围),那么你可以使用一个带有多个值的IN子句:

SELECT count( *)
FROM   TABLEA
WHERE  (refyear, refmonth) IN (
         (2020, 1), (2020, 2), (2020, 3), (2020, 4), (2020, 5),
         (2019, 1), (2019, 2), (2019, 3), (2019, 4), (2019, 5), (2019, 6), (2019, 7), (2019, 8)
       )

或者你可以使用你的查询,用OR替换AND

SELECT count(*)
FROM   TABLEA
WHERE  ( refyear= 2020 AND refmonth in (1,2,3,4,5) )
OR     ( refyear= 2019 AND refmonth in (1,2,3,4,5,6,7,8) )
  • 注:在这种情况下,额外的括号是不必要的,可以删除;但它们有助于可视化ANDOR运算符的优先级。*
5ssjco0h

5ssjco0h3#

create table TABLEA (
YEAR integer,
MONTH integer,
USERID integer,
NAME varchar(50));

insert into tablea values (2020, 1, 1, 'ABC');
insert into tablea values (2020, 2, 1, 'ABC');
insert into tablea values (2020, 3, 1, 'ABC');
insert into tablea values (2020, 4, 1, 'ABC');
insert into tablea values (2020, 5, 1, 'ABC2020');
insert into tablea values (2019, 1, 1, 'ABC');
insert into tablea values (2019, 2, 1, 'ABC');
insert into tablea values (2019, 3, 1, 'ABC');
insert into tablea values (2019, 4, 1, 'ABC');
insert into tablea values (2019, 5, 1, 'ABC');
insert into tablea values (2019, 6, 1, 'ABC');
insert into tablea values (2019, 7, 1, 'ABC');
insert into tablea values (2019, 8, 1, 'ABC');

select * from tablea;

 year | month | userid |  name
------+-------+--------+---------
 2020 |     1 |      1 | ABC
 2020 |     2 |      1 | ABC
 2020 |     3 |      1 | ABC
 2020 |     4 |      1 | ABC
 2020 |     5 |      1 | ABC2020
 2019 |     1 |      1 | ABC
 2019 |     2 |      1 | ABC
 2019 |     3 |      1 | ABC
 2019 |     4 |      1 | ABC
 2019 |     5 |      1 | ABC
 2019 |     6 |      1 | ABC
 2019 |     7 |      1 | ABC
 2019 |     8 |      1 | ABC

如果你想在一个查询结果中有多个选择的结果,你可以尝试这样做:

select count(*)
from tablea
where year=2020
and month in (1,2,3,4,5)
union
select count(*)
from tablea
where year= 2019
and month in (1,2,3,4,5,6,7,8);

  count
-------
     5
     8

相关问题