Question : Multiple CTE's in 1 Query - Not working

I'm trying to set reference to two different CTE's,
and no matter what combination of semi-colons, or commas I
employ; nothing seems to work.

Help?
thanks,
-JW
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
--'****************************************************************************'
--''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
--'PURPOSE:        - Query to Create a 'Table' housing
--'						1.) ASSET_ID
--'						2.) EVENT_TYPE
--'								a) -1 Exit
--'								b) 0 REO
--'								c) 1 Aquired
--'						3.)	EVENT_DATE
--'						4.)	EVENT_UPB
--'						5.)	EVENT_DESC
--'					Unioned together in 1 table, 5 columns
--'					instead of multiple tables, multiple Columns
--'
--'JOINS:			- Joins to ASSET_ID	
--'
--'
--'****************************************************************************'
--''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



WITH vxAST_MVT(
	ASSET_ID,
	EVENT_TYPE,
	EVENT_DATE,
	EVENT_UPB,
	EVENT_DESC) 
AS(

SELECT DISTINCT
	   a.ASSET_ID,
	   mvt.EVENT_TYPE,
	   mvt.EVENT_DATE,
	   mvt.EVENT_UPB,
	   mvt.EVENT_DESC

FROM V_US_ASSET a

INNER JOIN(	SELECT ASSET_ID,1 EVENT_TYPE,asset_acquisition_date EVENT_DATE,UPB_ACQ EVENT_UPB,TRANSFER_REASON EVENT_DESC
		FROM V_US_ASSET WHERE asset_acquisition_date IS NOT NULL AND TRANSFER_REASON IS NOT NULL

	UNION

		SELECT ASSET_ID,1,ASSET_REACTIVATION_DATE,UPB_AT_REACTIVATION,REACTIVATION_TRANSFER_REASON
		FROM V_US_ASSET WHERE ASSET_REACTIVATION_DATE IS NOT NULL AND REACTIVATION_TRANSFER_REASON IS NOT NULL

	UNION
							
		SELECT ASSET_ID,1,REACTIVATION_SS_TRANSFER_DATE_HISTORICAL,REACTIVATION_UPB_SS_TRANSFER_AMT_HISTORICAL,REACTIVATION_TRANSFER_REASON_HISTORICAL
		FROM V_US_ASSET WHERE REACTIVATION_SS_TRANSFER_DATE_HISTORICAL IS NOT NULL AND REACTIVATION_TRANSFER_REASON_HISTORICAL IS NOT NULL

	UNION
			SELECT ASSET_ID,-1,ACT_DISPO_DATE, UPB_DISPO, DISPO_FULL_TYPE
			FROM V_US_DISPOSITIONS  	
	UNION
			 SELECT ASSET_ID,-1 AS TYPES, act_tfr_master_serv_date,MOD_UPB,'RTM'			 
			 FROM V_US_RTM
	UNION 
			SELECT vprop.ASSET_ID,0,vreo.FC_DATE_ACTUAL,NULL,NULL
			FROM V_US_PROPERTY vprop INNER JOIN V_US_REO_FORECLOSURE vreo ON vreo.PROPERTY_SYS_ID = vprop.PROPERTY_SYS_ID)
	mvt

ON a.ASSET_ID = mvt.ASSET_ID),

--'****************************************************************************'
--''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
--'PURPOSE:        - Query to Create a 'Table' housing
--'						1.) ASSET_ID
--'						2.) Most Recent Valuation SUMMED AT Asset Level
--'						3.) Valuation Date
--'						4.) Valuation Type (Appraisal or BOV)
--'
--'
--'JOINS:			- Joins to ASSET_ID	
--'
--'
--'****************************************************************************'
--''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
WITH vxVAL(ASSET_ID,
				 VAL_VALUE,
				 VAL_DATE,
				 VAL_TYPE) AS 
(
SELECT vprop.ASSET_ID,
	   SUM(c.BOV_VALUE) AS VAL_VALUE,
	   MAX(c.BOV_DATE) AS VAL_DATE,
	   'BOV' AS VAL_TYPE

FROM V_US_PROPERTY vprop 
	INNER JOIN(SELECT a.PROPERTY_SYS_ID, a.BOV_DATE, a.BOV_VALUE
			   FROM BOV a
					INNER JOIN (SELECT 
									xa.PROPERTY_SYS_ID,
									MAX(xa.BOV_DATE) AS BOV_DATE
							    FROM BOV xa 
								GROUP BY xa.PROPERTY_SYS_ID) b 
					ON b.PROPERTY_SYS_ID = a.PROPERTY_SYS_ID
					AND a.BOV_DATE = b.BOV_DATE) c
ON c.PROPERTY_SYS_ID = vprop.PROPERTY_SYS_ID
GROUP BY vprop.ASSET_ID

UNION

SELECT vprop.ASSET_ID,
	   SUM(c.CURR_APP_VALUE) AS CURR_APP_VALUE,
	   MAX(c.CURR_APP_DATE) AS CURR_APP_DATE,
		'APP' AS TYPES

FROM V_US_PROPERTY vprop INNER JOIN(SELECT a.PROPERTY_SYS_ID, a.CURR_APP_DATE, a.CURR_APP_VALUE
									FROM APPRAISAL a
									INNER JOIN (SELECT 
												xa.PROPERTY_SYS_ID,
												MAX(xa.CURR_APP_DATE) AS CURR_APP_DATE
							        FROM APPRAISAL xa 
									GROUP BY xa.PROPERTY_SYS_ID) b 
						ON b.PROPERTY_SYS_ID = a.PROPERTY_SYS_ID
						AND a.CURR_APP_DATE = b.CURR_APP_DATE) c
ON c.PROPERTY_SYS_ID = vprop.PROPERTY_SYS_ID
GROUP BY vprop.ASSET_ID)
---------------------------------------------------------------------------------------

SELECT DISTINCT
	vast.ASSET_ID,
	MAX_ACQ.EVENT_DATE,
	vxVAL.VAL_VALUE

FROM V_US_ASSET vast

INNER JOIN (SELECT vxAST_MVT.ASSET_ID, MAX(vxAST_MVT.EVENT_DATE) EVENT_DATE
			FROM vxAST_MVT WHERE vxAST_MVT.EVENT_TYPE = 1
			GROUP BY vxAST_MVT.ASSET_ID) MAX_ACQ
	ON vast.ASSET_ID = MAX_ACQ.ASSET_ID

	LEFT OUTER JOIN vxVAL value
	ON value.ASSET_ID = vast.ASSET_ID AND
	value.TYPE = 'BOV'

Answer : Multiple CTE's in 1 Query - Not working

just need one WITH clause:

--'****************************************************************************'
--''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
--'PURPOSE:        - Query to Create a 'Table' housing
--'                                    1.) ASSET_ID
--'                                    2.) EVENT_TYPE
--'                                                a) -1 Exit
--'                                                b) 0 REO
--'                                                c) 1 Aquired
--'                                    3.)      EVENT_DATE
--'                                    4.)      EVENT_UPB
--'                                    5.)      EVENT_DESC
--'                              Unioned together in 1 table, 5 columns
--'                              instead of multiple tables, multiple Columns
--'
--'JOINS:                  - Joins to ASSET_ID      
--'
--'
--'****************************************************************************'
--''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



WITH vxAST_MVT(
      ASSET_ID,
      EVENT_TYPE,
      EVENT_DATE,
      EVENT_UPB,
      EVENT_DESC)
AS(

SELECT DISTINCT
         a.ASSET_ID,
         mvt.EVENT_TYPE,
         mvt.EVENT_DATE,
         mvt.EVENT_UPB,
         mvt.EVENT_DESC

FROM V_US_ASSET a

INNER JOIN(      SELECT ASSET_ID,1 EVENT_TYPE,asset_acquisition_date EVENT_DATE,UPB_ACQ EVENT_UPB,TRANSFER_REASON EVENT_DESC
            FROM V_US_ASSET WHERE asset_acquisition_date IS NOT NULL AND TRANSFER_REASON IS NOT NULL

      UNION

            SELECT ASSET_ID,1,ASSET_REACTIVATION_DATE,UPB_AT_REACTIVATION,REACTIVATION_TRANSFER_REASON
            FROM V_US_ASSET WHERE ASSET_REACTIVATION_DATE IS NOT NULL AND REACTIVATION_TRANSFER_REASON IS NOT NULL

      UNION
                                          
            SELECT ASSET_ID,1,REACTIVATION_SS_TRANSFER_DATE_HISTORICAL,REACTIVATION_UPB_SS_TRANSFER_AMT_HISTORICAL,REACTIVATION_TRANSFER_REASON_HISTORICAL
            FROM V_US_ASSET WHERE REACTIVATION_SS_TRANSFER_DATE_HISTORICAL IS NOT NULL AND REACTIVATION_TRANSFER_REASON_HISTORICAL IS NOT NULL

      UNION
                  SELECT ASSET_ID,-1,ACT_DISPO_DATE, UPB_DISPO, DISPO_FULL_TYPE
                  FROM V_US_DISPOSITIONS        
      UNION
                   SELECT ASSET_ID,-1 AS TYPES, act_tfr_master_serv_date,MOD_UPB,'RTM'                   
                   FROM V_US_RTM
      UNION
                  SELECT vprop.ASSET_ID,0,vreo.FC_DATE_ACTUAL,NULL,NULL
                  FROM V_US_PROPERTY vprop INNER JOIN V_US_REO_FORECLOSURE vreo ON vreo.PROPERTY_SYS_ID = vprop.PROPERTY_SYS_ID)
      mvt

ON a.ASSET_ID = mvt.ASSET_ID),

--'****************************************************************************'
--''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
--'PURPOSE:        - Query to Create a 'Table' housing
--'                                    1.) ASSET_ID
--'                                    2.) Most Recent Valuation SUMMED AT Asset Level
--'                                    3.) Valuation Date
--'                                    4.) Valuation Type (Appraisal or BOV)
--'
--'
--'JOINS:                  - Joins to ASSET_ID      
--'
--'
--'****************************************************************************'
--''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
vxVAL(ASSET_ID,
                         VAL_VALUE,
                         VAL_DATE,
                         VAL_TYPE) AS
(
SELECT vprop.ASSET_ID,
         SUM(c.BOV_VALUE) AS VAL_VALUE,
         MAX(c.BOV_DATE) AS VAL_DATE,
         'BOV' AS VAL_TYPE

FROM V_US_PROPERTY vprop
      INNER JOIN(SELECT a.PROPERTY_SYS_ID, a.BOV_DATE, a.BOV_VALUE
                     FROM BOV a
                              INNER JOIN (SELECT
                                                      xa.PROPERTY_SYS_ID,
                                                      MAX(xa.BOV_DATE) AS BOV_DATE
                                              FROM BOV xa
                                                GROUP BY xa.PROPERTY_SYS_ID) b
                              ON b.PROPERTY_SYS_ID = a.PROPERTY_SYS_ID
                              AND a.BOV_DATE = b.BOV_DATE) c
ON c.PROPERTY_SYS_ID = vprop.PROPERTY_SYS_ID
GROUP BY vprop.ASSET_ID

UNION

SELECT vprop.ASSET_ID,
         SUM(c.CURR_APP_VALUE) AS CURR_APP_VALUE,
         MAX(c.CURR_APP_DATE) AS CURR_APP_DATE,
            'APP' AS TYPES

FROM V_US_PROPERTY vprop INNER JOIN(SELECT a.PROPERTY_SYS_ID, a.CURR_APP_DATE, a.CURR_APP_VALUE
                                                      FROM APPRAISAL a
                                                      INNER JOIN (SELECT
                                                                        xa.PROPERTY_SYS_ID,
                                                                        MAX(xa.CURR_APP_DATE) AS CURR_APP_DATE
                                                  FROM APPRAISAL xa
                                                      GROUP BY xa.PROPERTY_SYS_ID) b
                                    ON b.PROPERTY_SYS_ID = a.PROPERTY_SYS_ID
                                    AND a.CURR_APP_DATE = b.CURR_APP_DATE) c
ON c.PROPERTY_SYS_ID = vprop.PROPERTY_SYS_ID
GROUP BY vprop.ASSET_ID)
---------------------------------------------------------------------------------------

SELECT DISTINCT
      vast.ASSET_ID,
      MAX_ACQ.EVENT_DATE,
      vxVAL.VAL_VALUE

FROM V_US_ASSET vast

INNER JOIN (SELECT vxAST_MVT.ASSET_ID, MAX(vxAST_MVT.EVENT_DATE) EVENT_DATE
                  FROM vxAST_MVT WHERE vxAST_MVT.EVENT_TYPE = 1
                  GROUP BY vxAST_MVT.ASSET_ID) MAX_ACQ
      ON vast.ASSET_ID = MAX_ACQ.ASSET_ID

      LEFT OUTER JOIN vxVAL value
      ON value.ASSET_ID = vast.ASSET_ID AND
      value.TYPE = 'BOV'
Random Solutions  
 
programming4us programming4us