CTE内部有多个子查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CTE内部有多个子查询相关的知识,希望对你有一定的参考价值。

我想知道是否有人知道为什么我不能将下面的代码转换成CTE?

是因为它有太多的子查询?请协助。我打算使用以下查询的结果作为临时表来加入。

当我运行下面的代码而不添加“WITH CTE as(”语法运行完美,但是一旦我添加语法,我得到一个错误:

BIC00004。 DAL010059。访问数据库时发生错误:ILLEGAL SYMBOL“”。一些可能是法律的符号是:SELECT(,。SQLCODE = -104,SQLSTATE = 42601,DRIVER = 4.17.29扩展错误描述是:在SELECT(,“)之后发现了一个意外的标记”“。预期的标记可能包括: “{2}”

我很感激帮助。

    SELECT F.PBC_NUMBER, F.EY_SAMPLE, F.DISTRIBUTION_CENTER, F.SITE_ID, F.NIIN, MIN(F.CAT) AS CAT
    FROM (
        SELECT DISTINCT E.PBC_NUMBER, E.EY_SAMPLE, E.DISTRIBUTION_CENTER, E.SITE_ID, E.NIIN, CONCAT(E.TRX_DT, E.SEQ_TIME) AS CAT
        FROM (
            SELECT C.PBC_NUMBER, C.EY_SAMPLE, C.NIIN, C.DISTRIBUTION_CENTER, C.SITE_ID, C.TRX_DT, MIN(D.SEQ_TIME) AS SEQ_TIME
            FROM (
                SELECT A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDRVENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'HERV'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDJFENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID IN ('HEJF', 'HEJG')
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDCNENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018225' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'HECN'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDDEENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018225' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'EGDE'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDWGENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018226' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'HEWG'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDAAENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018233' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'HEAA'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDDKENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'YKBK'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDPJENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'YYAL'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME) AS C
            INNER JOIN 
            (
                SELECT A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDRVENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'HERV'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDJFENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID IN ('HEJF', 'HEJG')
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDCNENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018225' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'HECN'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDDEENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018225' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'EGDE'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDWGENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018226' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'HEWG'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDAAENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018233' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'HEAA'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDDKENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'YKBK'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
                UNION

                SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
                FROM KCA0001.TRANS12 A
                LEFT OUTER JOIN DDPJENVR.ATR B
                ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
                WHERE A.SITE_ID = 'YYAL'
                GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT) AS D


            ON C.NIIN = D.NIIN AND C.SITE_ID = D.SITE_ID AND C.TRX_DT = D.TRX_DT AND C.SEQ_TIME = D.SEQ_TIME
            GROUP BY C.PBC_NUMBER, C.EY_SAMPLE, C.TRX_DT, C.DISTRIBUTION_CENTER, C.SITE_ID, C.NIIN) AS E
            ORDER BY E.SITE_ID) AS F
            GROUP BY F.PBC_NUMBER, F.EY_SAMPLE, F.DISTRIBUTION_CENTER, F.SITE_ID, F.NIIN 
答案

我删除了Aaron Dietz所说的ORDER BY并且能够查询。

` SELECT F.PBC_NUMBER, F.EY_SAMPLE, F.DISTRIBUTION_CENTER, F.SITE_ID, F.NIIN, MIN(F.CAT) AS CAT
FROM (
    SELECT DISTINCT E.PBC_NUMBER, E.EY_SAMPLE, E.DISTRIBUTION_CENTER, E.SITE_ID, E.NIIN, CONCAT(E.TRX_DT, E.SEQ_TIME) AS CAT
    FROM (
        SELECT C.PBC_NUMBER, C.EY_SAMPLE, C.NIIN, C.DISTRIBUTION_CENTER, C.SITE_ID, C.TRX_DT, MIN(D.SEQ_TIME) AS SEQ_TIME
        FROM (
            SELECT A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDRVENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'HERV'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDJFENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID IN ('HEJF', 'HEJG')
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDCNENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018225' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'HECN'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDDEENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018225' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'EGDE'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDWGENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018226' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'HEWG'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDAAENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018233' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'HEAA'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDDKENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'YKBK'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, MIN(B.TRX_DT) AS TRX_DT, A.NIIN, B.SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDPJENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'YYAL'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.SEQ_TIME) AS C
        INNER JOIN 
        (
            SELECT A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDRVENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'HERV'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDJFENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID IN ('HEJF', 'HEJG')
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDCNENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018225' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'HECN'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDDEENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018225' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'EGDE'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDWGENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018226' AND '2018229') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'HEWG'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDAAENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018233' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'HEAA'
            GROUP BY A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, A.NIIN, B.MED_STA_CD, B.TRX_DT
            UNION

            SELECT  A.PBC_NUMBER, A.EY_SAMPLE, A.DISTRIBUTION_CENTER, A.SITE_ID, B.TRX_DT, A.NIIN, MIN(B.SEQ_TIME) AS SEQ_TIME
            FROM KCA0001.TRANS12 A
            LEFT OUTER JOIN DDDKENVR.ATR B
            ON B.SITE_ID = A.SITE_ID AND B.NIIN = A.NIIN AND (B.TRX_DT BETWEEN '2018232' AND '2018236') AND B.DOC_ID IN ('D8A', 'D9A')
            WHERE A.SITE_ID = 'YKBK'
            GROUP BY A.PBC_NUM

以上是关于CTE内部有多个子查询的主要内容,如果未能解决你的问题,请参考以下文章

CTE 和子查询的区别?

为啥 CTE 比游标/派生表/子查询/临时表等更好?

SQL Server 子查询/CTE 建议

CTE、子查询、临时表或表变量之间是不是存在性能差异?

在 CTE 或子查询中优化

单个查询中的多个 CTE