Oracle中子查询的问题纠正了查询?

时间:2014-12-24 17:11:48

标签: sql oracle

SELECT COUNT(DISTINCT SEC.ERROR_GROUP_ID),
  COUNT(DISTINCT SEC_DET.ERROR_GROUP_ID),
  COUNT(DISTINCT MB.ERROR_GROUP_ID),
  COUNT(DISTINCT OD.ERROR_GROUP_ID),
  (SELECT COUNT (DISTINCT SEC_SCH.ERROR_GROUP_ID)
    FROM SCHEMA.SECURITY SEC
    LEFT OUTER JOIN SCHEMA.SECURITY_SCHEDULE SEC_SCH
     ON SEC.MSD_SECURITY_ID     =SEC_SCH.MSD_SECURITY_ID
     WHERE SEC.MSD_SECURITY_ID IN
      ( SELECT DISTINCT main.MSD_SECURITY_ID
         FROM SCHEMA2.Positions main
          WHERE main.QUANTITY != 0
          AND systimestamp    >= main.eff_from_dt
          AND main.eff_to_dt   > systimestamp
          AND systimestamp    >= main.asrt_from_dt
          AND main.asrt_to_dt  > systimestamp
     )) 

FROM SCHEMA.SECURITY SEC
JOIN SCHEMA.SECURITY_DETAIL SEC_DET
ON SEC.MSD_SECURITY_ID = SEC_DET.MSD_SECURITY_ID
LEFT OUTER JOIN SCHEMA.MUNI_BOND MB
ON SEC.MSD_SECURITY_ID=MB.MSD_SECURITY_ID
LEFT OUTER JOIN SCHEMA.OPTION_DETAIL OD
ON SEC.MSD_SECURITY_ID =OD.MSD_SECURITY_ID
WHERE SEC.MSD_SECURITY_ID IN
  ( SELECT DISTINCT main.MSD_SECURITY_ID
  FROM SCHEMA2.Positions main
  WHERE main.QUANTITY != 0
  AND systimestamp    >= main.eff_from_dt
  AND main.eff_to_dt   > systimestamp
  AND systimestamp    >= main.asrt_from_dt
  AND main.asrt_to_dt  > systimestamp
  ) ;

错误ORA-00936:缺少表达式 00936. 00000 - “缺少表达” *原因:
*行动: 行错误:365列:3

嵌套查询语法需要为此纠正才能解决我遇到的问题吗?

2 个答案:

答案 0 :(得分:1)

这是一个部分答案 - 我完全不了解你的一些代码。我认为它有问题。

注意:**表示粗体 - 我搞砸了格式化**不是这个SQL的一部分。

你必须分组。在这种情况下:

(SELECT DISTINCT (SEC_SCH.ERROR_GROUP_ID)
    FROM SCHEMA.SECURITY SEC
    LEFT OUTER JOIN SCHEMA.SECURITY_SCHEDULE SEC_SCH
     ON SEC.MSD_SECURITY_ID     =SEC_SCH.MSD_SECURITY_ID
     WHERE SEC.MSD_SECURITY_ID IN
      ( SELECT DISTINCT main.MSD_SECURITY_ID
         FROM SCHEMA2.Positions main
          WHERE main.QUANTITY != 0
          AND systimestamp    >= main.eff_from_dt
          AND main.eff_to_dt   > systimestamp
          AND systimestamp    >= main.asrt_from_dt
          AND main.asrt_to_dt  > systimestamp
     )) **foo**

    FROM SCHEMA.SECURITY SEC
    JOIN SCHEMA.SECURITY_DETAIL SEC_DET
    ON SEC.MSD_SECURITY_ID = SEC_DET.MSD_SECURITY_ID
    LEFT OUTER JOIN SCHEMA.MUNI_BOND MB
    ON SEC.MSD_SECURITY_ID=MB.MSD_SECURITY_ID
    LEFT OUTER JOIN SCHEMA.OPTION_DETAIL OD
    ON SEC.MSD_SECURITY_ID =OD.MSD_SECURITY_ID
    WHERE SEC.MSD_SECURITY_ID IN
      ( SELECT DISTINCT main.MSD_SECURITY_ID
      FROM SCHEMA2.Positions main
      WHERE main.QUANTITY != 0
      AND systimestamp    >= main.eff_from_dt
      AND main.eff_to_dt   > systimestamp
      AND systimestamp    >= main.asrt_from_dt
      AND main.asrt_to_dt  > systimestamp
      )
    **group by foo** ;

答案 1 :(得分:0)

您的整体查询是一个没有group by的聚合查询,因此需要返回一行。您在select中有一个可以返回多行的子查询 - 我怀疑问题与此结构有关。

我建议您将distinct更改为聚合函数。但是什么? COUNT(DISTINCT SEC_SCH.ERROR_GROUP_ID))? MAX(SEC_SCH.ERROR_GROUP_ID))? LISTAGG(SEC_SCH.ERROR_GROUP_ID, ',') WITHIN GROUP (ORDER BY SEC_SCH.ERROR_GROUP_ID))?我不知道。目前尚不清楚第三栏的内容。

您的整个查询看起来很可疑。如此多的count(disintct)表达式通常意味着您正在加入独立的维度 - 创建笛卡尔积。很难说这是否是一个问题,因为没有样本数据和期望的结果,你的问题实际上并没有说明你想要完成什么。