0

このクエリを作成するためのより良い方法を探していますが、私の SQL スキルは基本的なものではありません。これを改善するための指針を探しています。これは最初の 2 列のみで、完全なレポートにはさらに 10 列が含まれます。

特定の修理タイプのセットを取得し、カウントと計算を使用してそれらを分析しています。1 番目は当会計年度に繰り越されたジョブであり、2 番目は現在受け入れられているジョブの合計額です。

SELECT
    "Type",
    (
        SELECT
            NVL (COUNT(jjo.jjobno), 0)
        FROM
            jjobh jjo
        WHERE
            jjo.jclcode = 'L'
        AND jjo.jstatus <> '6'
        AND jjo.year_rec <> (
            SELECT
                sub_code
            FROM
                code_table
            WHERE
                main_code = 'YEAR'
        )
        AND (
            week_comp IS NULL
            OR year_comp = (
                SELECT
                    sub_code
                FROM
                    code_table
                WHERE
                    main_code = 'YEAR'
            )
        )
        AND jjo.jrepair_type = "Type"
    ) AS "B/F",
    (
        SELECT
            NVL (COUNT(jjo.jjobno), 0)
        FROM
            jjobh jjo
        WHERE
            jjo.jclcode = 'L'
        AND jjo.jstatus <> '6'
        AND jjo.year_rec = (
            SELECT
                sub_code
            FROM
                code_table
            WHERE
                main_code = 'YEAR'
        )
        AND jjo.jrepair_type = "Type"
    ) AS "Recvd"
FROM
    (
        SELECT
            rep.repair_type_code AS "Type"
        FROM
            repair_type rep
        WHERE
            rep.client = 'L'
        AND rep.work_centre = '004682'
        ORDER BY
            rep.repair_type_code
    )
ORDER BY
    "Type";
4

1 に答える 1

0

Your code is a mess. I suspect you want something like:

SELECT jjo.jrepair_type, count(*) as valbf
FROM (SELECT coalesce(COUNT(jjo.jjobno), 0)
      FROM jjobh jjo cross join
           (SELECT sub_code
            FROM code_table
            WHERE main_code = 'YEAR'
           ) sc
      WHERE jjo.jclcode = 'L' AND
            jjo.jstatus <> '6' AND
            jjo.year_rec <> sc.sub_code AND
            (week_comp IS NULL OR
             year_comp = sc.sub_code
            ) 
     ) jjo join
     (SELECT rep.repair_type_code AS "Type"
      FROM repair_type rep
      WHERE rep.client = 'L' AND
            rep.work_centre = '004682'
     ) rtc
     on jjo.jrepair_type = rtc.repair_type_code
group by jjo.jrepair_type;

It looks like you want to join the "jjo" table to the "repair type code" table, producing information about each repair type. The order by in the subquery is useless.

My suggestion is to move the "jjo" table to the outer "from". You should also move the WHERE clauses to the outermost WHERE clause (which I didn't do). I haven't quite figured out the date logic, but this might get you on the right track.

于 2012-07-31T12:58:54.233 に答える