1

我有这个由以前的开发人员编写的 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
4

2 回答 2

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
于 2012-08-08T10:25:30.960 回答
1

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

于 2012-08-08T10:21:43.100 回答