/* Bom Compare */
SELECT 
       pkg.PKG_ID,
       pv.PV_ID AS PROD_ID,
	   qry.OS_ID,		
	   pkg.PKG_NAME AS PROD_NAME,
	   qry.NODE_ICON,
	   qry.PROD_LOCATION,	   
	   pv.PKG_VERSION AS PROD_VERSION,
	   qry.OLD_PKG_VERSION,
	   qry.pv_id AS OLD_PROD_ID,
	   ( CASE 
	     WHEN qry.CHANGE_TYPE = 'A' THEN
	   		'LIMG_ADDED'
		 WHEN qry.CHANGE_TYPE = 'U' THEN
			'LIMG_UPDATED'
		 ELSE
		 	'LIMG_REMOVED'
		 END ) AS DIFF_ICON			 
  FROM (		   
		/* ADDED PRODUCTS ONLY */
		SELECT DECODE( oldbom.PROD_ID,
					   NULL, 'A', 'U' ) AS CHANGE_TYPE, 
			   newbom.PROD_ID, newbom.PROD_LOCATION, oldbom.OLD_PKG_VERSION, 
			   newbom.NEW_PKG_VERSION, newbom.OS_ID, newbom.NODE_ICON, oldbom.pv_id
		  FROM (
		  		SELECT osc.PROD_ID,
					   nn.NODE_NAME ||'&nbsp;/&nbsp;'|| os.OS_NAME ||'&nbsp;/&nbsp;' AS PROD_LOCATION,
					   pv.PKG_ID,
					   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT,
					   NULL AS OLD_PKG_VERSION,
					   pv.PKG_VERSION AS NEW_PKG_VERSION,
					   osc.OS_ID,
					   nt.NODE_ICON
				  FROM OS_CONTENTS osc,
				  	   OPERATING_SYSTEMS os,
					   NETWORK_NODES nn,
					   NODE_TYPES nt,
					   BOM_CONTENTS bc,
					   PACKAGE_VERSIONS pv
				 WHERE osc.OS_ID = os.OS_ID
				   AND os.NODE_ID = nn.NODE_ID
				   AND bc.NODE_ID = nn.NODE_ID
				   AND osc.PROD_ID = pv.PV_ID
				   AND nt.NODE_TYPE_ID = nn.NODE_TYPE_ID
				   AND pv.IS_PATCH IS NULL
				   AND bc.BOM_ID = :BOM_ID	
		  	   ) newbom,
			   (
			   	SELECT osc.PROD_ID,
					   nn.NODE_NAME ||'&nbsp;/&nbsp;'|| os.OS_NAME ||'&nbsp;/&nbsp;' AS PROD_LOCATION,
					   pv.PKG_ID,
					   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT,
					   pv.PKG_VERSION AS OLD_PKG_VERSION,
					   NULL AS NEW_PKG_VERSION,
					   pv.PV_ID,
					   osc.OS_ID,
					   nt.NODE_ICON
				  FROM OS_CONTENTS osc,
				  	   OPERATING_SYSTEMS os,
					   NETWORK_NODES nn,
					   NODE_TYPES nt,
					   BOM_CONTENTS bc,
					   PACKAGE_VERSIONS pv
				 WHERE osc.OS_ID = os.OS_ID
				   AND os.NODE_ID = nn.NODE_ID
				   AND bc.NODE_ID = nn.NODE_ID
				   AND nt.NODE_TYPE_ID = nn.NODE_TYPE_ID
				   AND osc.PROD_ID = pv.PV_ID
				   AND pv.IS_PATCH IS NULL
				   AND bc.BOM_ID = :COMPARE_BOM_ID	
			   ) oldbom
		 WHERE newbom.PROD_LOCATION = oldbom.PROD_LOCATION (+) 
		   --AND newbom.PROD_ID = oldbom.PROD_ID (+)
		   AND newbom.PKG_ID  = oldbom.PKG_ID (+)
		   AND newbom.V_EXT  = oldbom.V_EXT (+) 
		   AND ( oldbom.PROD_ID != newbom.PROD_ID  OR oldbom.PROD_ID IS NULL )
		   AND newbom.PROD_ID NOT IN 
		   (
		   	   SELECT osc.PROD_ID
			   FROM OS_CONTENTS osc,
			  	    OPERATING_SYSTEMS os,
					NETWORK_NODES nn,
					NODE_TYPES nt,
					BOM_CONTENTS bc,
					PACKAGE_VERSIONS pv
			  WHERE osc.OS_ID = os.OS_ID
				AND os.NODE_ID = nn.NODE_ID
				AND bc.NODE_ID = nn.NODE_ID
				AND nt.NODE_TYPE_ID = nn.NODE_TYPE_ID
				AND osc.PROD_ID = pv.PV_ID
				AND pv.IS_PATCH IS NULL
				AND bc.BOM_ID = :COMPARE_BOM_ID	
		  )	   

		/*UNION
		
		 REMOVED PRODUCTS ONLY
		SELECT 'R' AS CHANGE_TYPE, oldbom.PROD_ID, oldbom.PROD_LOCATION,
		oldbom.OLD_PKG_VERSION, 
		newbom.NEW_PKG_VERSION, newbom.OS_ID, newbom.NODE_ICON, oldbom.pv_id
		  FROM (
		  		SELECT osc.PROD_ID,
					   nn.NODE_NAME ||'&nbsp;/&nbsp;'|| os.OS_NAME ||'&nbsp;/&nbsp;' AS PROD_LOCATION,
					   pv.PKG_ID,
					   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT,
					   NULL AS OLD_PKG_VERSION,
					   pv.PKG_VERSION AS NEW_PKG_VERSION,
					   osc.OS_ID,
					   nt.NODE_ICON
				  FROM OS_CONTENTS osc,
				  	   OPERATING_SYSTEMS os,
					   NETWORK_NODES nn,
					   NODE_TYPES nt,
					   BOM_CONTENTS bc,
					   PACKAGE_VERSIONS pv
				 WHERE osc.OS_ID = os.OS_ID
				   AND os.NODE_ID = nn.NODE_ID
				   AND bc.NODE_ID = nn.NODE_ID
				   AND nt.NODE_TYPE_ID = nn.NODE_TYPE_ID
				   AND osc.PROD_ID = pv.PV_ID
				   AND pv.IS_PATCH IS NULL
				   AND bc.BOM_ID = :BOM_ID	
		  	   ) newbom,
			   (
			   	SELECT osc.PROD_ID,
					   nn.NODE_NAME ||'&nbsp;/&nbsp;'|| os.OS_NAME ||'&nbsp;/&nbsp;' AS PROD_LOCATION,
					   pv.PKG_ID,
					   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT,
					   pv.PKG_VERSION AS OLD_PKG_VERSION,
					   NULL AS NEW_PKG_VERSION,
					   pv.pv_id,
					   osc.OS_ID,
					   nt.NODE_ICON
				  FROM OS_CONTENTS osc,
				  	   OPERATING_SYSTEMS os,
					   NETWORK_NODES nn,
					   NODE_TYPES nt,
					   BOM_CONTENTS bc,
					   PACKAGE_VERSIONS pv
				 WHERE osc.OS_ID = os.OS_ID
				   AND os.NODE_ID = nn.NODE_ID
				   AND bc.NODE_ID = nn.NODE_ID
				   AND nt.NODE_TYPE_ID = nn.NODE_TYPE_ID
				   AND osc.PROD_ID = pv.PV_ID
				   AND pv.IS_PATCH IS NULL
				   AND bc.BOM_ID = :COMPARE_BOM_ID	
			   ) oldbom
		 WHERE newbom.PROD_LOCATION (+) = oldbom.PROD_LOCATION 
		   --AND newbom.PROD_ID (+) = oldbom.PROD_ID
		   AND newbom.PKG_ID (+) = oldbom.PKG_ID
		   AND newbom.V_EXT (+) = oldbom.V_EXT   
		   AND newbom.PROD_LOCATION IS NULL */
       ) qry,
	   PACKAGE_VERSIONS pv,
	   PACKAGES pkg,
	   PRODUCT_DETAILS pd  
 WHERE qry.PROD_ID = pv.PV_ID
   AND pv.PKG_ID = pkg.PKG_ID
   AND pd.PROD_ID (+) = qry.PROD_ID
 ORDER BY UPPER(pkg.PKG_NAME)   	