在没有外部的情况下开始案例陈述 'Select' - CTE

Start a Case Statement without an outside 'Select' - CTE's

运行 此查询(下)returns 一个 'Too Many Values' 错误:

select 
case
     when to_char(sysdate, 'yyyymmdd') = to_char(sysdate, 'yyyymm') || '01'
     then (select FirstReportGroups.*, FirstReportDetails.*
           from FirstReportGroups, FirstReportDetails)

     when to_char(sysdate, 'yyyymmdd') = to_char(sysdate, 'yyyymm') || '16'
     then (select SecondReportGroups.*, SecondReportDetails.*
           from SecondReportGroups, SecondReportDetails)
end as LetsSee
from cmtmpentered t1 join cmtmpconf t2    
      on t1.casenumber = t2.casenumber    
      and t1.enc = t2.enc 
;

我正在使用 CTE(它们不包括在内,因为它会使它变得很长)并且这对我来说是合乎逻辑的,但是谷歌搜索这个 'Too Many Values' 错误没有给我任何实质性帮助。 运行 CTE 单独工作,所以这不是问题。

我想如果我能去掉外面的 'Select' 语句并且只将选择保留在 Case 中,一切都会得到解决。如果我解释得不好,我正在寻找的一个例子是:

case
     when to_char(sysdate, 'yyyymmdd') = to_char(sysdate, 'yyyymm') || '01'
     then (select FirstReportGroups.*, FirstReportDetails.*
           from FirstReportGroups, FirstReportDetails)

     when to_char(sysdate, 'yyyymmdd') = to_char(sysdate, 'yyyymm') || '16'
     then (select SecondReportGroups.*, SecondReportDetails.*
           from SecondReportGroups, SecondReportDetails)
end as LetsSee
;

这在任何情况下都可行吗?这种语法显然不起作用,否则我不会问这个问题 - 但有没有相关的方法可以做到这一点?

您没有为 LetsSee 指定连接条件。您有效地交叉连接了 LetsSeefrom 子句的结果:

from cmtmpentered t1 
    join cmtmpconf t2 on t1.casenumber = t2.casenumber and t1.enc = t2.enc

我建议您加入 LetsSeet1t2

此外,您没有为对指定联接条件:

  • FirstReportGroupsFirstReportDetails
  • SecondReportGroupsSecondReportDetails
select FirstReportGroups.*, FirstReportDetails.*
from       (select 1 a from dual) FirstReportGroups
cross join (select 2 b from dual) FirstReportDetails
where extract(day from sysdate) = 1
---------
union all
---------
select SecondReportGroups.*, SecondReportDetails.*
from       (select 3 a from dual) SecondReportGroups
cross join (select 4 b from dual) SecondReportDetails
where extract(day from sysdate) = 16;
  1. 用内联视图替换了常见的 table 表达式。 只有在多次引用 CTE 时才应使用它们。对于只习惯于过程代码的程序员来说,使用小示例它们可能看起来更好一些。严重的 SQL 需要多层嵌套的内联视图。如果没有 CTE,调试会容易得多 - CTE 很难突出显示和 运行 代码子块。
  2. 用谓词替换 case 表达式以按日期过滤。 CASE 表达式只能 return 一个值。您可能可以对类型做一些花哨的事情,但这会非常复杂。我的代码仍然假设两组 return 具有相同类型的值。如果不是这样,您需要在应用程序级别做一些不同的事情。
  3. to_char 替换为 extract 日期处理在 Oracle 中可能会令人困惑。诀窍是将所有内容都保留在它们的原始类型中。除了显示格式之外,您几乎不需要使用 to_charto_date。对于任何其他操作,几乎可以肯定有一个函数可以在不转换类型的情况下处理它。
  4. 用 ANSI 语法 cross join. 替换了 , ANSI 风格的语法有几个优点。一个主要优点是它清楚地表明交叉连接是有意的。