如何分解这个很长的sql来简化?

时间:2012-08-08 10:06:30

标签: c# sql oracle

我有一个由前一个开发人员编写的这个sql(下面)。我怎样才能将它分成三个或四个部分再次连接在一起而不是这个单一的大sql?请指教。我是sql的新手,不知道如何简化它。谢谢。我正在使用C#控制台应用程序。

SELECT distinct
  department_name CTG_DEPARTMENT,
  chart_id SPC_CHART_ID,
  concat(process_id, concat('-', item_name)) MONITOR_ITEM,
  product_id CTG_PRODUCT, 
  graphtype SPC_GRAPH_TYPE,
  CASE WHEN viorules LIKE '%9%'  THEN 'OOS'
       WHEN viorules LIKE '%10%' THEN 'OOS'
       WHEN viorules IS NULL     THEN 'OK'
                                 ELSE 'OOC' END SPC_STATUS,
  process_stage,
  'L8B' fab_name 
FROM
(
  SELECT
    'ARRAY' process_stage,
     dept.item_name department_name,
     dept.chart_id,
     proc_id.item_name process_id,
     item.item_name,
     product.item_name product_id, 
     graphtype
  FROM 
  (
    SELECT
      item_name,
      chart_id
    FROM
      aryspch.c_ctg_filter f,
      aryspch.c_ctg_item i,
      aryspch.c_ctg_relate r
    WHERE
      GROUP_ID = 6 
      AND filter_name IN ('Department')
      AND f.filter_id = i.filter_id
      AND i.item_id = r.item_id
  ) dept
  ,
  (
    SELECT
      item_name,
      chart_id
    FROM
      aryspch.c_ctg_filter f,
      aryspch.c_ctg_item   i,
      aryspch.c_ctg_relate r
    WHERE
      GROUP_ID = 6
      AND filter_name IN ('Process_ID')    
      AND f.filter_id = i.filter_id
      AND i.item_id = r.item_id
  ) proc_id
  ,
  (
    SELECT
      item_name,
      chart_id
    FROM
      aryspch.c_ctg_filter f,
      aryspch.c_ctg_item   i,
      aryspch.c_ctg_relate r
    WHERE
      GROUP_ID = 6
      AND filter_name IN ('Item')
      AND f.filter_id = i.filter_id
      AND i.item_id = r.item_id
  ) item
  ,
  (
    SELECT
      item_name,
      chart_id
    FROM
      aryspch.c_ctg_filter f,
      aryspch.c_ctg_item   i,
      aryspch.c_ctg_relate r
    WHERE
      GROUP_ID = 6
      AND filter_name IN ('Product')
      AND f.filter_id = i.filter_id
      AND i.item_id = r.item_id
  ) product
  ,
  (
    SELECT DISTINCT
      chartid,
      graphtype
    FROM
      aryspch.spchis
    WHERE
      reporttime > SYSDATE - 2
  ) a
  WHERE
        dept.chart_id = proc_id.chart_id
    AND dept.chart_id = item.chart_id
    AND dept.chart_id = product.chart_id
    AND dept.chart_id = a.chartid
)
  spc
LEFT JOIN
(
  SELECT
    viorules,
    mtimestamp,
    chartid
  FROM
    aryspch.oochis
)
  oochis
    ON  spc.chart_id = oochis.chartid
    AND mtimestamp > SYSDATE - 2

2 个答案:

答案 0 :(得分:2)

一种选择是使用公用表表达式来定义每个子查询,然后将这些命名查询连接在一起。

WITH
  dept AS
(
  SELECT
    item_name,
    chart_id
  FROM
    aryspch.c_ctg_filter f,
    aryspch.c_ctg_item i,
    aryspch.c_ctg_relate r
  WHERE
    GROUP_ID = 6 
    AND filter_name IN ('Department')
    AND f.filter_id = i.filter_id
    AND i.item_id = r.item_id
)
,
  proc_id  AS
(
  SELECT
    item_name,
    chart_id
  FROM
    aryspch.c_ctg_filter f,
    aryspch.c_ctg_item   i,
    aryspch.c_ctg_relate r
  WHERE
    GROUP_ID = 6
    AND filter_name IN ('Process_ID')    
    AND f.filter_id = i.filter_id
    AND i.item_id = r.item_id
)
,
  item AS
(
  SELECT
    item_name,
    chart_id
  FROM
    aryspch.c_ctg_filter f,
    aryspch.c_ctg_item   i,
    aryspch.c_ctg_relate r
  WHERE
    GROUP_ID = 6
    AND filter_name IN ('Item')
    AND f.filter_id = i.filter_id
    AND i.item_id = r.item_id
)
,
  product AS
(
  SELECT
    item_name,
    chart_id
  FROM
    aryspch.c_ctg_filter f,
    aryspch.c_ctg_item   i,
    aryspch.c_ctg_relate r
  WHERE
    GROUP_ID = 6
    AND filter_name IN ('Product')
    AND f.filter_id = i.filter_id
    AND i.item_id = r.item_id
)
,
  a AS
(
  SELECT DISTINCT
    chartid,
    graphtype
  FROM
    aryspch.spchis
  WHERE
    reporttime > SYSDATE - 2
)
,
  spc AS
(
  SELECT
    'ARRAY' process_stage,
     dept.item_name department_name,
     dept.chart_id,
     proc_id.item_name process_id,
     item.item_name,
     product.item_name product_id, 
     graphtype
  FROM 
    dept,
    proc_id,
    item,
    product,
    a
  WHERE
        dept.chart_id = proc_id.chart_id
    AND dept.chart_id = item.chart_id
    AND dept.chart_id = product.chart_id
    AND dept.chart_id = a.chartid
)
,
  oochis AS
(
  SELECT
    viorules,
    mtimestamp,
    chartid
  FROM
    aryspch.oochis
)

SELECT distinct
  department_name CTG_DEPARTMENT,
  chart_id SPC_CHART_ID,
  concat(process_id, concat('-', item_name)) MONITOR_ITEM,
  product_id CTG_PRODUCT, 
  graphtype SPC_GRAPH_TYPE,
  CASE WHEN viorules LIKE '%9%'  THEN 'OOS'
       WHEN viorules LIKE '%10%' THEN 'OOS'
       WHEN viorules IS NULL     THEN 'OK'
                                 ELSE 'OOC' END SPC_STATUS,
  process_stage,
  'L8B' fab_name 
FROM
  spc
LEFT JOIN
  oochis
    ON  spc.chart_id = oochis.chartid
    AND mtimestamp > SYSDATE - 2

答案 1 :(得分:1)

您可以创建视图 http://msdn.microsoft.com/en-us/library/ms187956.aspx(CREATE VIEW(Transact-SQL)) 对于每个“from”内容,并对视图执行选择。