with Table As QueryTable用法及实例
常用的语法结构为:
With T1 AS (QueryTable:查询语句),
T2 AS (QueryTable:查询语句),
……
Tn AS (QueryTable:查询语句)
+ QueryTable :表集合
其中:QueryTable表示select 语法结构,返回一个数据集
用法实例:
WITH T1 AS (SELECT SPAP_TYPE,GEND_NAME,SPAP_CUST,SPAP_DATE,CM_SORT,SPAP_NBR,SPAPD_LINE,SPAPD_PART,PT_DESC1,SPAPD_QTY,SO_NBR FROM SPAP_MSTR LEFT JOIN SPAPD_DET ON SPAP_NBR=SPAPD_NBR LEFT JOIN CM_MSTR ON CM_ADDR=SPAP_CUST LEFT JOIN SO_MSTR ON SPAPD_SOD_NBR=SO_NBR LEFT JOIN GEND_DET ON GEND_OPTION=SPAP_TYPE AND GEND_GEN='BJ_TYPE' LEFT JOIN PT_MSTR ON PT_PART=SPAPD_PART WHERE 1=1 AND SPAP_PST=1 UNION ALL SELECT SPAP_TYPE,B.GEND_NAME,SO_CUST,SO_ORD_DATE,CM_SORT,SO_NBR,SOD_LINE,SOD_PART,PT_DESC1,SOD_QTY_ORD,SO_NBR FROM SO_MSTR LEFT JOIN SOD_DET ON SO_NBR=SOD_NBR LEFT JOIN SPAP_MSTR ON SPAP_NBR=SO_FC_NBR LEFT JOIN CM_MSTR ON CM_ADDR=SO_CUST LEFT JOIN GEND_DET B ON B.GEND_GEN='BJ_TYPE' AND B.GEND_OPTION=SPAP_TYPE AND B.GEND_DISABLED=0 LEFT JOIN PT_MSTR ON PT_PART=SOD_PART WHERE SO_PST=1 AND SO_PROG_CODE='SLSOMTA3' ) ,--售后申请单 T2 AS (SELECT MOVD_MOV,MOVD_LINE MOVD_LINE1,MOVD_SRC_LINE MOVD_SRC_LINE1,MOVD_SRC_NBR,MOVD_QTY,MOV_PST FROM MOVD_DET LEFT JOIN MOV_MSTR ON MOVD_MOV=MOV_MOV WHERE 1=1 and MOV_PROG_CODE='SHSBMTA1' ) ,--领料单 T3 AS (SELECT MOVD_MOV,MOVD_LINE MOVD_LINE2,MOVD_SRC_LINE MOVD_SRC_LINE2,MOVD_SRC_NBR,MOVD_QTY,MOV_PST FROM MOV_MSTR LEFT JOIN MOVD_DET ON MOVD_MOV=MOV_MOV WHERE MOV_PROG_CODE='SHSBMTA2' ) ,--入库单 T4 AS (SELECT RPLD_NBR,RPLD_LINE,RPLD_SRC_NBR,RPLD_SRC_LINE,RPLD_QTY,RPL_PST FROM RPL_MSTR LEFT JOIN RPLD_DET ON RPL_NBR=RPLD_NBR WHERE RPL_PROG_CODE='SHSBMTA4') ,--退料单 T5 AS (SELECT DND_DN,DND_LINE,DND_DNA_LINE,DND_DNA,DND_QTY_SHIP,DN_PST FROM DN_MSTR LEFT JOIN DND_DET ON DN_DN=DND_DN WHERE DN_PROG_CODE='SHSBMTA3') ,--出库单 T6 AS (SELECT RPLD_NBR,RPLD_LINE,RPLD_SRC_NBR,RPLD_SRC_LINE,RPLD_QTY,RPL_PST FROM RPL_MSTR LEFT JOIN RPLD_DET ON RPL_NBR=RPLD_NBR WHERE RPL_PROG_CODE='SHSBMTA5') --返厂单 SELECT DISTINCT T1.SPAP_TYPE, T1.GEND_NAME, T1.SPAP_CUST, T1.CM_SORT, T1.SPAP_NBR, T1.SPAPD_LINE, T1.SPAPD_PART, T1.PT_DESC1, T4.RPLD_SRC_NBR, NVL(T1.SPAPD_QTY,0) SPAPD_QTY, T1.SPAP_DATE, NVL(T1.SO_NBR,'') AS SO_NBR, NVL(T2.MOVD_MOV,'') AS MOV_MOV1, NVL(TO_CHAR(T2.MOVD_LINE1),'') AS MOVD_LINE1, NVL(TO_CHAR(T2.MOVD_QTY),'') AS MOVD_QTY1, DECODE(T2.MOV_PST,0,'未过账',1,'已过账') AS MOV_PST1, NVL(T3.MOVD_MOV,'') AS MOV_MOV2, NVL(TO_CHAR(T3.MOVD_LINE2),'') AS MOVD_LINE2, NVL(TO_CHAR(T3.MOVD_QTY),'') AS MOVD_QTY2, DECODE(T3.MOV_PST,0,'未过账',1,'已过账') AS MOV_PST2, NVL(T4.RPLD_NBR,'') AS RPL_NBR1, NVL(TO_CHAR(T4.RPLD_LINE),'') AS RPLD_LINE1, NVL(TO_CHAR(T4.RPLD_QTY),'') AS RPLD_QTY1, DECODE(T4.RPL_PST,0,'未过账',1,'已过账') AS RPL_PST1, NVL(T5.DND_DN,'') DN_DN, NVL(TO_CHAR(T5.DND_LINE),'') DND_LINE, NVL(TO_CHAR(T5.DND_QTY_SHIP),'') DND_QTY_SHIP, DECODE(T5.DN_PST,0,'未过账',1,'已过账') AS DN_PST, NVL(T6.RPLD_NBR,'') AS RPL_NBR2, NVL(TO_CHAR(T6.RPLD_LINE),'') AS RPLD_LINE2, NVL(TO_CHAR(T6.RPLD_QTY),'') AS RPLD_QTY2, DECODE(T6.RPL_PST,0,'未过账',1,'已过账') AS RPL_PST2 FROM T1 JOIN T2 ON T2.MOVD_SRC_NBR=T1.SPAP_NBR AND T2.MOVD_SRC_LINE1=t1.SPAPD_LINE LEFT JOIN T3 ON T3.MOVD_SRC_NBR=T2.MOVD_MOV AND T3.MOVD_SRC_LINE2=T2.MOVD_LINE1 LEFT JOIN T4 ON (T4.RPLD_SRC_NBR=T2.MOVD_MOV AND T4.RPLD_SRC_LINE=T2.MOVD_LINE1) OR (T4.RPLD_SRC_NBR = T3.MOVD_MOV AND T4.RPLD_SRC_LINE = T3.MOVD_LINE2) LEFT JOIN T5 ON T5.DND_DNA=T2.MOVD_MOV AND T5.DND_DNA_LINE=T2.MOVD_LINE1 LEFT JOIN T6 ON T6.RPLD_SRC_NBR=T5.DND_DN AND T6.RPLD_SRC_LINE=T5.DND_LINE WHERE 1=1 AND T1.SPAP_DATE >= :SPAP_DATE_FR AND T1.SPAP_DATE <= :SPAP_DATE_TO AND between2(T1.SPAP_CUST, :SPAP_CUST_FR, :SPAP_CUST_TO) = 1 AND between2(T1.SPAP_NBR, :SPAP_NBR_FR, :SPAP_NBR_TO) = 1 AND between2(T1.SPAPD_PART, :SPAPD_PART_FR, :SPAPD_PART_TO) = 1 AND between2(T1.SPAP_NBR, :DN_DN_FR, :DN_DN_TO) = 1 ORDER BY SPAP_NBR ASC
参考链接:http://www.360doc.com/content/14/0704/10/12414131_391906799.shtml
与你共亲到无可亲密时,便知友谊万岁是尽头。