Oracle:选择Min with Where子句

时间:2015-04-28 10:24:40

标签: oracle oracle11g where-clause min

我正在尝试针对Oracle数据库运行以下语句并获取缺少的表达式消息。我怀疑问题是因为MINOVERLAP和MAXOVERLAP的SELECT不能将OVERLAPS识别为表,但我现在卡住了。我在一个名为Qlikview的工具中运行此查询,因此没有创建临时表等的好处。如果可能的话,它需要作为单个查询完成。

SELECT UCR, TR,
SELECT(MIN(OVERLAP_START_DT)
FROM OVERLAPS
WHERE OVERLAPS.UCR=UCR
AND OVERLAPS.TR  <=TR) AS MINOVERLAP
SELECT(MAX(OVERLAP_END_DT)
FROM OVERLAPS
WHERE OVERLAPS.UCR=UCR
AND OVERLAPS.TR  <=TR) AS MAXOVERLAP
FROM
  (SELECT DISTINCT REF_WORKFLOW.UCR,
    REF_WORKFLOW.TR,
    OVERLAP_START_DT,
    OVERLAP_END_DT
  FROM CDW.REF_WORKFLOW
  JOIN CDW.REF_WEL_PREV_TR_OVERLAP
  ON REF_WEL_PREV_TR_OVERLAP.WORKFLOW_SID=REF_WORKFLOW.WORKFLOW_SID
  JOIN CDW.REF_TRANS_PART_LINE
  ON REF_TRANS_PART_LINE.UCR       =REF_WORKFLOW.UCR
  AND REF_TRANS_PART_LINE.TR       =REF_WORKFLOW.TR
  AND REF_TRANS_PART_LINE.BUREAU_ID='SY'
  JOIN CDW.REF_WORKFLOW_EVENT
  ON REF_WORKFLOW_EVENT.TRANS_PART_LINE_SID            =REF_TRANS_PART_LINE.TRANS_PART_LINE_SID
  WHERE REF_WORKFLOW.BUREAU_ID                         = 'SY'
  AND REF_TRANS_PART_LINE.PARTICIPANT_TRANS_ROLE_CODE IN('AGREEMENT PARTY','LEAD')
  AND REF_WEL_PREV_TR_OVERLAP.OVERLAP_ELAPSED_DURATION >0
  AND REF_WORKFLOW_EVENT.WORKFLOW_EVENT_DT            >= TO_DATE('24/04/2015','DD/MM/YYYY')
  ) OVERLAPS;

3 个答案:

答案 0 :(得分:0)

您在第5行缺少逗号:

SELECT UCR, TR,
SELECT(MIN(OVERLAP_START_DT)
FROM OVERLAPS
WHERE OVERLAPS.UCR=UCR
AND OVERLAPS.TR  <=TR) AS MINOVERLAP, -- <<< here
SELECT(MAX(OVERLAP_END_DT)
FROM OVERLAPS
WHERE OVERLAPS.UCR=UCR
AND OVERLAPS.TR  <=TR) AS MAXOVERLAP
FROM
  (SELECT DISTINCT REF_WORKFLOW.UCR,
    REF_WORKFLOW.TR,
    OVERLAP_START_DT,
    OVERLAP_END_DT
  FROM CDW.REF_WORKFLOW
  JOIN CDW.REF_WEL_PREV_TR_OVERLAP
  ON REF_WEL_PREV_TR_OVERLAP.WORKFLOW_SID=REF_WORKFLOW.WORKFLOW_SID
  JOIN CDW.REF_TRANS_PART_LINE
  ON REF_TRANS_PART_LINE.UCR       =REF_WORKFLOW.UCR
  AND REF_TRANS_PART_LINE.TR       =REF_WORKFLOW.TR
  AND REF_TRANS_PART_LINE.BUREAU_ID='SY'
  JOIN CDW.REF_WORKFLOW_EVENT
  ON REF_WORKFLOW_EVENT.TRANS_PART_LINE_SID            =REF_TRANS_PART_LINE.TRANS_PART_LINE_SID
  WHERE REF_WORKFLOW.BUREAU_ID                         = 'SY'
  AND REF_TRANS_PART_LINE.PARTICIPANT_TRANS_ROLE_CODE IN('AGREEMENT PARTY','LEAD')
  AND REF_WEL_PREV_TR_OVERLAP.OVERLAP_ELAPSED_DURATION >0
  AND REF_WORKFLOW_EVENT.WORKFLOW_EVENT_DT            >= TO_DATE('24/04/2015','DD/MM/YYYY')
  ) OVERLAPS;

答案 1 :(得分:0)

  

获取缺少的表达式消息

您正在使用内嵌视图,但缺少逗号

下面:

AND OVERLAPS.TR  <=TR) AS MINOVERLAP
SELECT(MAX(OVERLAP_END_DT)

为什么不使用 WITH 子句a.k.a 子查询保理功能。对于MIN和MAX计算,请使用with子句,然后在过滤谓词中使用它。

答案 2 :(得分:0)

除了缺少逗号之外,您的子查询需要在括号中正确括起来;而你已经拥有的是不平衡的。或者说,在错误的地方;您目前开始使用SELECT(MIN...(SELECT MIN...SELECT。将左括号移到子查询的SELECT UCR, TR, (SELECT MIN(OVERLAP_START_DT) FROM OVERLAPS WHERE OVERLAPS.UCR=UCR AND OVERLAPS.TR <=TR) AS MINOVERLAP, (SELECT MAX(OVERLAP_END_DT) FROM OVERLAPS WHERE OVERLAPS.UCR=UCR AND OVERLAPS.TR <=TR) AS MAXOVERLAP FROM (SELECT DISTINCT ... ) OVERLAPS; 之前,而不是添加另一组。

所以你需要:

WITH OVERLAPS AS (
SELECT DISTINCT ...
)
SELECT UCR, TR,
(SELECT MIN(OVERLAP_START_DT)
FROM OVERLAPS
WHERE OVERLAPS.UCR=UCR
AND OVERLAPS.TR  <=TR) AS MINOVERLAP,
(SELECT MAX(OVERLAP_END_DT)
FROM OVERLAPS
WHERE OVERLAPS.UCR=UCR
AND OVERLAPS.TR  <=TR) AS MAXOVERLAP
FROM OVERLAPS;

但是您怀疑,您的OVERLAPS内联视图不在这些子查询的范围内。如果您的工具支持(有些很挑剔),您可以使用CTE:

WHERE OVERLAPS.UCR=UCR

但你的逻辑仍有缺陷。我认为你正试图做相关,但你的条件总是如此;例如:

SELECT UCR, TR,
  MIN(OVERLAP_START_DT) OVER (PARTITION BY UCR ORDER BY TR
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS MINOVERLAP,
  MAX(OVERLAP_END_DT) OVER (PARTITION BY UCR ORDER BY TR
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS MAXOVERLAP
FROM
  (SELECT DISTINCT ...
  ) OVERLAPS;

...将每行中的值与自身进行比较,因此将始终求值为true(除非该值为null)。

您可能会发现使用带有窗口子句的min / max分析版本更简单,使用CTE或内联视图:

WITH OVERLAPS (UCR, TR, OVERLAP_START_DT, OVERLAP_END_DT) AS (
  SELECT DISTINCT ...
)
SELECT UCR, TR,
  (SELECT MIN(O2.OVERLAP_START_DT)
  FROM OVERLAPS O2
  WHERE O2.UCR = OVERLAPS.UCR
  AND O2.TR <= OVERLAPS.TR) AS MINOVERLAP,
  (SELECT MAX(O2.OVERLAP_END_DT)
  FROM OVERLAPS O2
  WHERE O2.UCR = OVERLAPS.UCR
  AND O2.TR <= OVERLAPS.TR) AS MAXOVERLAP
FROM OVERLAPS;

大部分未经测试,因为我们没有任何数据或预期结果,但希望能给你一些指示。

SQL Fiddle显示带有子查询的CTE,由于上述相关问题导致结果不正确;和分析方法。显然,使用一些组成的数据行。

如果由于某种原因无法使用分析函数,则可以通过在子查询中对CTE进行别名来修复相关性问题:

//a/@rev[not(contains(.,'thumb'))] | //img/@src[not(contains(.,'thumb'))]

SQL Fiddle