需要可行的解决方案来解决我的无效条件创建
Need viable solution to my invalid where criteria creation
我需要 select 上一季度的三个月名称,并将这些月份名称应用到我的 where 语句中。
我无法使以下内容正常工作:
WHERE table.column in
(CASE WHEN to_char(sysdate, 'MM') in (01,02,03) THEN ('OCTOBER','NOVEMBER','DECEMBER')
WHEN to_char(sysdate, 'MM') in (04,05,06) THEN ('JANUARY','FEBRUARY','MARCH')
WHEN to_char(sysdate, 'MM') in (07,08,09) THEN ('APRIL','MAY','JUNE')
WHEN to_char(sysdate, 'MM') in (10,11,12) THEN ('JULY', 'AUGUST', 'SEPTEMBER')
END)
一个 case
表达式不能 return 多个结果。但你可以这样做:
WHERE CASE WHEN to_char(sysdate, 'MM') in ('01','02','03') THEN case when table.column in ('OCTOBER','NOVEMBER','DECEMBER') then 1 else 0 end
WHEN to_char(sysdate, 'MM') in ('04','05','06') THEN case when table.column in('JANUARY','FEBRUARY','MARCH') then 1 else 0 end
WHEN to_char(sysdate, 'MM') in ('07','08','09') THEN case when table.column in('APRIL','MAY','JUNE') then 1 else 0 end
WHEN to_char(sysdate, 'MM') in ('10','11','12') THEN case when table.column in('JULY', 'AUGUST', 'SEPTEMBER') then 1 else 0 end
END = 1
请注意,我还明确地将数字转换为字符串,那是因为 to_char
也将是 return 字符串,您希望避免隐式转换。
您似乎想要从现在起 9 个月后的季度中的月份。如果是这样,您可以使用:
where to_char(to_date(month_name,'MONTH'),'Q') =
to_char(add_months(sysdate,9),'Q');
我需要 select 上一季度的三个月名称,并将这些月份名称应用到我的 where 语句中。 我无法使以下内容正常工作:
WHERE table.column in
(CASE WHEN to_char(sysdate, 'MM') in (01,02,03) THEN ('OCTOBER','NOVEMBER','DECEMBER')
WHEN to_char(sysdate, 'MM') in (04,05,06) THEN ('JANUARY','FEBRUARY','MARCH')
WHEN to_char(sysdate, 'MM') in (07,08,09) THEN ('APRIL','MAY','JUNE')
WHEN to_char(sysdate, 'MM') in (10,11,12) THEN ('JULY', 'AUGUST', 'SEPTEMBER')
END)
一个 case
表达式不能 return 多个结果。但你可以这样做:
WHERE CASE WHEN to_char(sysdate, 'MM') in ('01','02','03') THEN case when table.column in ('OCTOBER','NOVEMBER','DECEMBER') then 1 else 0 end
WHEN to_char(sysdate, 'MM') in ('04','05','06') THEN case when table.column in('JANUARY','FEBRUARY','MARCH') then 1 else 0 end
WHEN to_char(sysdate, 'MM') in ('07','08','09') THEN case when table.column in('APRIL','MAY','JUNE') then 1 else 0 end
WHEN to_char(sysdate, 'MM') in ('10','11','12') THEN case when table.column in('JULY', 'AUGUST', 'SEPTEMBER') then 1 else 0 end
END = 1
请注意,我还明确地将数字转换为字符串,那是因为 to_char
也将是 return 字符串,您希望避免隐式转换。
您似乎想要从现在起 9 个月后的季度中的月份。如果是这样,您可以使用:
where to_char(to_date(month_name,'MONTH'),'Q') =
to_char(add_months(sysdate,9),'Q');