联合和分组的麻烦

时间:2014-09-26 14:19:53

标签: sql oracle

我知道此前这是一个话题,但我仍然无法在工会内进行分组。我看过这里:Grouping and Joining a Unioned Table. Having Problems等等,我仍然对如何处理括号感到困惑。这是我的代码:

SELECT
      W.WORK_ORDER_NO,
      W.WORK_CLASS,
      SD.WORK_ORDER_TASK_NO,
      sd.ATTRIBUTE_VALUE"Outage Type",
      sd2.attribute_value"Total Time Water Off",
      WT.TASK_STATUS,
      wt.area,
      wt.creation_date,
      sd3.attribute_value"Homes Affected",
      sd4.attribute_value"Businesses Affected",
      sum(sm.CT_REGULAR_HOURS)"Reg Hours",
      sum(sm.CT_PREMIUM_HOURS)"OT Hours"
    FROM
    (
    SELECT
      W.WORK_ORDER_NO,
      W.WORK_CLASS,
      SD.WORK_ORDER_TASK_NO,
      sd.ATTRIBUTE_VALUE"Outage Type",
      sd2.attribute_value"Total Time Water Off",
      WT.TASK_STATUS,
      wt.area,
      wt.creation_date,
      sd3.attribute_value"Homes Affected",
      sd4.attribute_value"Businesses Affected",
      sm.CT_REGULAR_HOURS"Reg Hours",
      sm.CT_PREMIUM_HOURS"OT Hours"
    FROM
      SA_WORK_ORDER W,
      SA_WORK_ORDER_TASK WT,
      SA_SERVICE_HISTORY_DETAILS SD,
      SA_SERVICE_HISTORY_DETAILS SD2,
      SA_SERVICE_HISTORY_DETAILS SD3,
      SA_SERVICE_HISTORY_DETAILS SD4,
      smu_transactions sm
    WHERE
      SD.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND W.WORK_ORDER_NO         = WT.WORK_ORDER_NO
    AND
      (
        WT.WORK_ORDER_NO        = SD.WORK_ORDER_NO
      AND WT.WORK_ORDER_TASK_NO = SD.WORK_ORDER_TASK_NO
      )
    AND
      (
        sd.work_order_no        = sd2.work_order_no
      AND sd.work_order_task_no = sd2.work_order_task_no
      )
    AND
      (
        sd.work_order_no        = sd3.work_order_no
      AND sd.work_order_task_no = sd3.work_order_task_no
      )
    AND
      (
        sd.work_order_no        = sd4.work_order_no
      AND sd.work_order_task_no = sd4.work_order_task_no
      )
    AND SD.SPECIFICATION_CATEGORY  = 'WATER OUTAGE'
    AND sd.attribute_desc          = 'Type of Outage'
    AND SD2.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd2.attribute_desc         = 'Total Time Water Off'
    AND SD3.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd3.attribute_desc         = 'Number of Homes affected'
    AND SD4.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd4.attribute_desc         = 'Number of Businesses affected'
    AND WT.TASK_STATUS            != 'CANCELED'
    AND sm.transaction_type        = 'CT'

    UNION ALL

    SELECT
      W.WORK_ORDER_NO,
      W.WORK_CLASS,
      SD.WORK_ORDER_TASK_NO,
      sd.ATTRIBUTE_VALUE"Outage Type",
      sd2.attribute_value"Total Time Water Off",
      WT.TASK_STATUS,
      wt.area,
      wt.creation_date,
      sd3.attribute_value"Homes Affected",
      sd4.attribute_value"Businesses Affected",
      SUM(sm.CT_REGULAR_HOURS)"Reg Hours",
      SUM(sm.CT_PREMIUM_HOURS)"OT Hours"
    FROM
    (  SELECT
        W.WORK_ORDER_NO,
        W.WORK_CLASS,
        SD.WORK_ORDER_TASK_NO,
        sd.ATTRIBUTE_VALUE"Outage Type",
        sd2.attribute_value"Total Time Water Off",
        WT.TASK_STATUS,
        wt.area,
        wt.creation_date,
        sd3.attribute_value"Homes Affected",
        sd4.attribute_value"Businesses Affected",
        sm.CT_REGULAR_HOURS"Reg Hours",
        sm.CT_PREMIUM_HOURS"OT Hours"
    FROM
      SA_WORK_HISTORY W,
      SA_WORK_HISTORY_TASK WT,
      SA_WORK_HISTORY_SERVICE_DETAIL SD,
      SA_WORK_HISTORY_SERVICE_DETAIL SD2,
      SA_WORK_HISTORY_SERVICE_DETAIL SD3,
      SA_WORK_HISTORY_SERVICE_DETAIL SD4,
      smu_transactions sm
    WHERE
      SD.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND W.WORK_ORDER_NO         = WT.WORK_ORDER_NO
    AND
      (
        WT.WORK_ORDER_NO        = SD.WORK_ORDER_NO
      AND WT.WORK_ORDER_TASK_NO = SD.WORK_ORDER_TASK_NO
      )
    AND
      (
        sd.work_order_no        = sd2.work_order_no
      AND sd.work_order_task_no = sd2.work_order_task_no
      )
    AND
      (
        sd.work_order_no        = sd3.work_order_no
      AND sd.work_order_task_no = sd3.work_order_task_no
      )
    AND
      (
        sd.work_order_no        = sd4.work_order_no
      AND sd.work_order_task_no = sd4.work_order_task_no
      )
    AND
      (
        sd.work_order_no        = sm.WORK_ORDER_NO
      AND sd.work_order_task_no = sm.work_order_task_no
      )
    AND SD.SPECIFICATION_CATEGORY  = 'WATER OUTAGE'
    AND sd.attribute_desc          = 'Type of Outage'
    AND SD2.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd2.attribute_desc         = 'Total Time Water Off'
    AND SD3.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd3.attribute_desc         = 'Number of Homes affected'
    AND SD4.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd4.attribute_desc         = 'Number of Businesses affected'
    AND WT.TASK_STATUS            != 'CANCELED'
    AND sm.transaction_type        = 'CT'
    )
    GROUP BY
      sm.CT_REGULAR_HOURS,
      sm.CT_PREMIUM_HOURS
      ORDER BY
      sd.work_order_no,
      sd.WORK_ORDER_TASK_NO;

错误是: ORA-00907:缺少右括号 00907. 00000 - "错过右括号" *原因:
*行动: 行错误:152列:23

我已经尝试过括号位置的许多不同排列并理解它应该如何工作但我仍然无法掌握它们的位置(或者如果这是问题)。

TIA可以提供任何帮助!

2 个答案:

答案 0 :(得分:1)

我不确定你真正想要什么,但是有一些关于分组的错误,我还添加了一个新的右括号,固定查询:

SELECT
      W.WORK_ORDER_NO,
      W.WORK_CLASS,
      SD.WORK_ORDER_TASK_NO,
      sd.ATTRIBUTE_VALUE"Outage Type",
      sd2.attribute_value"Total Time Water Off",
      WT.TASK_STATUS,
      wt.area,
      wt.creation_date,
      sd3.attribute_value"Homes Affected",
      sd4.attribute_value"Businesses Affected",
      sum(sm.CT_REGULAR_HOURS)"Reg Hours",
      sum(sm.CT_PREMIUM_HOURS)"OT Hours"
    FROM
    (
    SELECT
      W.WORK_ORDER_NO,
      W.WORK_CLASS,
      SD.WORK_ORDER_TASK_NO,
      sd.ATTRIBUTE_VALUE"Outage Type",
      sd2.attribute_value"Total Time Water Off",
      WT.TASK_STATUS,
      wt.area,
      wt.creation_date,
      sd3.attribute_value"Homes Affected",
      sd4.attribute_value"Businesses Affected",
      sm.CT_REGULAR_HOURS"Reg Hours",
      sm.CT_PREMIUM_HOURS"OT Hours"
    FROM
      SA_WORK_ORDER W,
      SA_WORK_ORDER_TASK WT,
      SA_SERVICE_HISTORY_DETAILS SD,
      SA_SERVICE_HISTORY_DETAILS SD2,
      SA_SERVICE_HISTORY_DETAILS SD3,
      SA_SERVICE_HISTORY_DETAILS SD4,
      smu_transactions sm
    WHERE
      SD.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND W.WORK_ORDER_NO         = WT.WORK_ORDER_NO
    AND
      (
        WT.WORK_ORDER_NO        = SD.WORK_ORDER_NO
      AND WT.WORK_ORDER_TASK_NO = SD.WORK_ORDER_TASK_NO
      )
    AND
      (
        sd.work_order_no        = sd2.work_order_no
      AND sd.work_order_task_no = sd2.work_order_task_no
      )
    AND
      (
        sd.work_order_no        = sd3.work_order_no
      AND sd.work_order_task_no = sd3.work_order_task_no
      )
    AND
      (
        sd.work_order_no        = sd4.work_order_no
      AND sd.work_order_task_no = sd4.work_order_task_no
      )
    AND SD.SPECIFICATION_CATEGORY  = 'WATER OUTAGE'
    AND sd.attribute_desc          = 'Type of Outage'
    AND SD2.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd2.attribute_desc         = 'Total Time Water Off'
    AND SD3.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd3.attribute_desc         = 'Number of Homes affected'
    AND SD4.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd4.attribute_desc         = 'Number of Businesses affected'
    AND WT.TASK_STATUS            != 'CANCELED'
    AND sm.transaction_type        = 'CT'
    )
    group by 
    W.WORK_ORDER_NO,
      W.WORK_CLASS,
      SD.WORK_ORDER_TASK_NO,
      sd.ATTRIBUTE_VALUE"Outage Type",
      sd2.attribute_value"Total Time Water Off",
      WT.TASK_STATUS,
      wt.area,
      wt.creation_date,
      sd3.attribute_value"Homes Affected",
      sd4.attribute_value

    UNION ALL

    SELECT
      W.WORK_ORDER_NO,
      W.WORK_CLASS,
      SD.WORK_ORDER_TASK_NO,
      sd.ATTRIBUTE_VALUE"Outage Type",
      sd2.attribute_value"Total Time Water Off",
      WT.TASK_STATUS,
      wt.area,
      wt.creation_date,
      sd3.attribute_value"Homes Affected",
      sd4.attribute_value"Businesses Affected",
      SUM(sm.CT_REGULAR_HOURS)"Reg Hours",
      SUM(sm.CT_PREMIUM_HOURS)"OT Hours"
    FROM
    (  SELECT
        W.WORK_ORDER_NO,
        W.WORK_CLASS,
        SD.WORK_ORDER_TASK_NO,
        sd.ATTRIBUTE_VALUE"Outage Type",
        sd2.attribute_value"Total Time Water Off",
        WT.TASK_STATUS,
        wt.area,
        wt.creation_date,
        sd3.attribute_value"Homes Affected",
        sd4.attribute_value"Businesses Affected",
        sm.CT_REGULAR_HOURS"Reg Hours",
        sm.CT_PREMIUM_HOURS"OT Hours"
    FROM
      SA_WORK_HISTORY W,
      SA_WORK_HISTORY_TASK WT,
      SA_WORK_HISTORY_SERVICE_DETAIL SD,
      SA_WORK_HISTORY_SERVICE_DETAIL SD2,
      SA_WORK_HISTORY_SERVICE_DETAIL SD3,
      SA_WORK_HISTORY_SERVICE_DETAIL SD4,
      smu_transactions sm
    WHERE
      SD.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND W.WORK_ORDER_NO         = WT.WORK_ORDER_NO
    AND
      (
        WT.WORK_ORDER_NO        = SD.WORK_ORDER_NO
      AND WT.WORK_ORDER_TASK_NO = SD.WORK_ORDER_TASK_NO
      )
    AND
      (
        sd.work_order_no        = sd2.work_order_no
      AND sd.work_order_task_no = sd2.work_order_task_no
      )
    AND
      (
        sd.work_order_no        = sd3.work_order_no
      AND sd.work_order_task_no = sd3.work_order_task_no
      )
    AND
      (
        sd.work_order_no        = sd4.work_order_no
      AND sd.work_order_task_no = sd4.work_order_task_no
      )
    AND
      (
        sd.work_order_no        = sm.WORK_ORDER_NO
      AND sd.work_order_task_no = sm.work_order_task_no
      )
    AND SD.SPECIFICATION_CATEGORY  = 'WATER OUTAGE'
    AND sd.attribute_desc          = 'Type of Outage'
    AND SD2.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd2.attribute_desc         = 'Total Time Water Off'
    AND SD3.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd3.attribute_desc         = 'Number of Homes affected'
    AND SD4.SPECIFICATION_CATEGORY = 'WATER OUTAGE'
    AND sd4.attribute_desc         = 'Number of Businesses affected'
    AND WT.TASK_STATUS            != 'CANCELED'
    AND sm.transaction_type        = 'CT'
    )
    GROUP BY
      W.WORK_ORDER_NO,
      W.WORK_CLASS,
      SD.WORK_ORDER_TASK_NO,
      sd.ATTRIBUTE_VALUE"Outage Type",
      sd2.attribute_value"Total Time Water Off",
      WT.TASK_STATUS,
      wt.area,
      wt.creation_date,
      sd3.attribute_value"Homes Affected",
      sd4.attribute_value
      ORDER BY 11, 12;

答案 1 :(得分:1)

我简化了您的查询以获得此伪查询,并且丢失的括号立即显现。

SELECT
  set_of_rows
FROM
(
SELECT
 set_of_rows
FROM
  set_of_tables
WHERE
  set_of_conditions ) //You missed this closing paranthesis
                    ^
UNION ALL

SELECT
  set_of_rows
FROM
(  SELECT
    set_of_rows
FROM
  set_of_tables
WHERE
  set_of_conditions
)
GROUP BY
  some_rows
ORDER BY
  some_other_rows