Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:37:58+00:00 2026-06-09T20:37:58+00:00

I have this sql (below) written by a previous developer. How can I break

  • 0

I have this sql (below) written by a previous developer. How can I break it up into three or four parts and join it together again instead of this single big sql? Please advise. I’m really new to sql and no idea how to simplify it. Thanks. I’m using C# console application.

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:38:00+00:00Added an answer on June 9, 2026 at 8:38 pm

    One option is to use Common Table Expressions to define each sub-query, and then to join those named queries together.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this SQL statement below INSERT into 2011_electricity ( date, energy, daynum) VALUES
I have written an SQL script having the below query. The query works fine.
Can we have multiple SQL* PLUS connections in a shell script? I have written
We have a function written in pl/sql(oracle) as below: CREATE OR REPLACE PROCEDURE folder_cycle_check
I have written some LINQ to simulate an SQL GroupBy statement (see below). However,
i have this sql query select * from table where name like ? but
I have this SQL statement: SELECT * FROM history WHERE (fk_person = 2119) AND
I have this sql string: Dim sqlQuery As String = SELECT TOP 1 ID,
I have this SQL query from the logs select content = 5%id%201=1 from pages
I have this SQL Query that pulls data from 3 tables. I am unable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.