包含多个子查询的CTE

时间:2018-08-29 20:36:58

标签: sql db2

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

是因为它有太多子查询吗?请协助。我计划使用以下查询的结果作为临时表进行连接。

当我运行下面的代码而未添加“ WTE CTE as(”语法时,它可以完美运行,但是一旦添加语法,我就会收到错误消息:

  

BIC00004。 DAL010059。访问数据库时发生错误:   非法符号“”。某些可能合法的符号   ARE: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 

1 个答案:

答案 0 :(得分:0)

我删除了亚伦·迪茨(Aaron Dietz)所述的ORDER BY,并能够进行查询。

DaggerAppComponent.builder().application(githubApp)
    .build().inject(githubApp)