Sql Oracle-ORA-01848错误
我正在尝试执行以下查询Sql Oracle-ORA-01848错误,sql,oracle,Sql,Oracle,我正在尝试执行以下查询 SELECT w.grid_no, SUM(A) FROM TABLE_1 w WHERE w.DAY between to_date((select jsow from TABLE_2 t where t.grid_no = w.grid_no and t.crop_no=90) || '-2016','DDD-YYYY') - 0 and to_date((select jsow from TABLE_2 t where t.grid_no = w.
SELECT w.grid_no, SUM(A)
FROM TABLE_1 w
WHERE w.DAY between to_date((select jsow from TABLE_2 t where t.grid_no = w.grid_no and t.crop_no=90) || '-2016','DDD-YYYY') - 0
and to_date((select jsow from TABLE_2 t where t.grid_no = w.grid_no and t.crop_no=90) || '-2016','DDD-YYYY') + 0 + 10 - 1
and A > 8
group BY w.GRID_NO
但我有一个错误
01848. 00000 - "day of year must be between 1 and 365 (366 for leap year)"
相反,如果我在grid_no上添加一个过滤器,查询就会工作;e、 g
SELECT w.grid_no, SUM(A)
FROM TABLE_1 w
WHERE w.grid_no=1000 -- <---- the added filter
and w.DAY between to_date((select jsow from TABLE_2 t where t.grid_no = w.grid_no and t.crop_no=90) || '-2016','DDD-YYYY') - 0
and to_date((select jsow from TABLE_2 t where t.grid_no = w.grid_no and t.crop_no=90) || '-2016','DDD-YYYY') + 0 + 10 - 1
and A > 8
jsow是一个朱利安日:对于给定的成对网格,表2中存储的最大值可以从1到366
我的目标是这样:对于表1中的每个网格,我必须在表2中得到相应的jsow值,并在通过jsow指定的范围内求和A值 如果表_2中的值为null,或者表_1中的网格_id在表_2中没有与crop 90匹配的行,则子查询将返回null或无数据。无论哪种方式,最终都会得到一个无效的数据字符串 使用一些虚拟数据:
create table table_1 (grid_no, day, a) as
select 1000, date '2016-01-01', 42 from dual
union all select 1001, date '2016-01-02', 42 from dual
union all select 1002, date '2016-01-03', 42 from dual
union all select 1003, date '2016-01-04', 42 from dual;
create table table_2(grid_no, crop_no, jsow) as
select 1000, 90, 1 from dual
union all select 1001, 91, 367 from dual
union all select 1002, 90, null from dual;
SELECT w.grid_no,
(select jsow from TABLE_2 t where t.grid_no = w.grid_no and t.crop_no=90) as jsow,
(select jsow from TABLE_2 t where t.grid_no = w.grid_no and t.crop_no=90) || '-2016' as date_string
FROM TABLE_1 w;
GRID_NO JSOW DATE_STRING
---------- ---------- ---------------------------------------------
1000 1 1-2016
1001 -2016
1002 -2016
1003 -2016
如果尝试将其中一个字符串转换为日期,则会出现相同的错误:
select to_date('-2016', 'DDD-YYYY') from dual;
ORA-01848: day of year must be between 1 and 365 (366 for leap year)
您可以使用联接(而不是两个子查询)重写查询,这使筛选所需行稍微容易一些:
SELECT w.grid_no, SUM(w.A)
FROM TABLE_1 w
JOIN TABLE_2 t ON t.grid_no = w.grid_no
AND w.DAY between to_date(t.jsow || '-2016','DDD-YYYY') - 0
AND to_date(t.jsow || '-2016','DDD-YYYY') + 9
WHERE t.crop_no=90
AND t.jsow between 1 and 366
AND w.A > 8
GROUP BY w.GRID_NO;
内部连接意味着忽略任何没有表_2行的表_1行;jsow上的过滤器在匹配项为空时启用。您可能更希望它成为一个外部连接,这取决于您实际执行的其他操作。错误表明您的jsow值实际上不在1和366之间?@AlexPoole我检查过:MINjsow=1,MAXjsow=366。grid_id=1000请参见第二个查询have jsow=366 grid id 1000显然具有OK jsow值,其他的一个没有。您是说从表2中选择minjsow、maxjsow,在所有网格ID中显示1和366,还是只选择1000?如果对所有id都是这样,那么在有和没有crop_no=90过滤器的情况下都是这样吗?@AlexPoole jsow=366,crop_no=90,grid_id=1000,对于crop 90,jsow是否有空值?或者表1中的网格ID在表2中没有与crop 90匹配的行?
SELECT w.grid_no, SUM(w.A)
FROM TABLE_1 w
JOIN TABLE_2 t ON t.grid_no = w.grid_no
AND w.DAY between to_date(t.jsow || '-2016','DDD-YYYY') - 0
AND to_date(t.jsow || '-2016','DDD-YYYY') + 9
WHERE t.crop_no=90
AND t.jsow between 1 and 366
AND w.A > 8
GROUP BY w.GRID_NO;