Oracle ORA:00907:在WHERE子句中使用大小写时缺少右括号
我必须每月根据日期列从表中提取一些数据。这个表每个月都会得到一些新记录,我必须编写一个查询,使用它我可以得到上个月添加的记录 查询是Oracle ORA:00907:在WHERE子句中使用大小写时缺少右括号,oracle,date-arithmetic,Oracle,Date Arithmetic,我必须每月根据日期列从表中提取一些数据。这个表每个月都会得到一些新记录,我必须编写一个查询,使用它我可以得到上个月添加的记录 查询是 select r.iar_start_date from reps r where case when (extract(month from sysdate) = 1) then ((extract(month from r.iar_start_date) = 12) and extract(year from r.iar_start_date) =
select r.iar_start_date
from reps r
where
case when (extract(month from sysdate) = 1)
then ((extract(month from r.iar_start_date) = 12) and extract(year from r.iar_start_date) = (extract(year from sysdate) - 1))
else (extract(month from r.iar_start_date) = (extract(month from sysdate) - 1) and extract(year from r.iar_start_date) = extract(year from sysdate))
end
此查询给出ORA:00907:缺少右括号
错误
任何帮助都是值得赞赏的 您不能有条件的
where
子句。您需要重新构造该子句,使其成为单个条件
可能是这样的:
where
(
extract(month from sysdate) = 1
and
extract(month from r.iar_start_date) = 12
and
extract(year from r.iar_start_date) = extract(year from sysdate) - 1
)
or
(
extract(month from sysdate) <> 1
and
extract(month from r.iar_start_date) = extract(month from sysdate) - 1
and
extract(year from r.iar_start_date) = extract(year from sysdate)
)
在哪里
(
摘录(从sysdate算起的月份)=1
和
摘录(从r.iar\U开始日期算起的月份)=12
和
摘录(从r.iar\u开始日期算起的年份)=摘录(从sysdate算起的年份)-1
)
或
(
摘录(从sysdate算起的月份)1
和
摘录(从r.iar\u开始日期算起的月份)=摘录(从sysdate算起的月份)-1
和
摘录(从r.iar\u开始日期算起的年份)=摘录(从sysdate算起的年份)
)
不能有这样的条件where
子句。您需要重新构造该子句,使其成为单个条件
可能是这样的:
where
(
extract(month from sysdate) = 1
and
extract(month from r.iar_start_date) = 12
and
extract(year from r.iar_start_date) = extract(year from sysdate) - 1
)
or
(
extract(month from sysdate) <> 1
and
extract(month from r.iar_start_date) = extract(month from sysdate) - 1
and
extract(year from r.iar_start_date) = extract(year from sysdate)
)
在哪里
(
摘录(从sysdate算起的月份)=1
和
摘录(从r.iar\U开始日期算起的月份)=12
和
摘录(从r.iar\u开始日期算起的年份)=摘录(从sysdate算起的年份)-1
)
或
(
摘录(从sysdate算起的月份)1
和
摘录(从r.iar\u开始日期算起的月份)=摘录(从sysdate算起的月份)-1
和
摘录(从r.iar\u开始日期算起的年份)=摘录(从sysdate算起的年份)
)
如果您想要的是上个月的行,那么您可以使用一些标准的日期操作技巧来实现更简单的条件:
where r.iar_start_date between trunc(add_months(sysdate,-1), 'MM')
and trunc(sysdate, 'MM') - (1/86400)
ADD_MONTHS()用于添加月份;使用负偏移减去月份
带有“MM”面具的TRUNC()表示一个月的第一天,即午夜
我们也可以用简单的算术来调整日期
-(1/86400)
从一天中减去一秒,在这种情况下产生前一天的最大日期时间值。如果您想要的是上个月的行,则可以使用一些标准的日期操作技巧来实现更简单的条件:
where r.iar_start_date between trunc(add_months(sysdate,-1), 'MM')
and trunc(sysdate, 'MM') - (1/86400)
ADD_MONTHS()用于添加月份;使用负偏移减去月份
带有“MM”面具的TRUNC()表示一个月的第一天,即午夜
我们也可以用简单的算术来调整日期<代码>-(1/86400)从一天中减去一秒,在这种情况下,产生前一天的最大日期时间值