-- UsedByThisSnapshotDetail.sql
-- Generate detailed data for projects that use this package version
--  Within an PARENT RELEASE specified by :RTAG_ID
--  Match control via :MATCH
--    :MATCH 0  - Match any  package name
--    :MATCH 1  - Match Package name and Project Suffix
--    :MATCH 2  - Match exactly the package. Name, Version and Suffix
 with 
  aa AS ( select NVL(v_ext,'LINK') as ext from package_versions where pv_id = :PV_ID),
  bb AS (select pkg_id from package_versions where pv_id = :PV_ID)
SELECT DECODE( ev.PKG_STATE, NULL, 0, ev.PKG_STATE ) AS PKG_STATE,
       rt.RTAG_ID,
       rt.RTAG_NAME,
       TO_CHAR(rt.created_stamp,'DD-Mon-YYYY HH24:MI:SS') AS created_stamp,
       rt.OFFICIAL,
       ev.ENV_AREA,
       pv.PV_ID,
       pkg.PKG_NAME,
       pv.PKG_VERSION,
       pv.DLOCKED,
       usr.FULL_NAME,
       usr.USER_EMAIL,
       pv.MODIFIED_STAMP  
  FROM ENVIRONMENT_VIEW ev,
       PACKAGE_VERSIONS pv,
       PACKAGES pkg,
       RELEASE_TAGS rt,
       USERS usr,
       aa,bb
 WHERE ev.PV_ID = pv.PV_ID
   AND pv.PKG_ID = pkg.PKG_ID
   AND ev.RTAG_ID = rt.RTAG_ID
   AND pv.MODIFIER_ID = usr.USER_ID
   AND rt.PARENT_RTAG_ID = :RTAG_ID
   AND (ev.PV_ID  = :PV_ID OR :MATCH != 2)
   AND (NVL(pv.v_ext,'LINK') in (  aa.ext ) OR :MATCH = 2 OR :MATCH = 0)
   AND (pv.PKG_ID in ( bb.pkg_id ) OR :MATCH = 2)
   AND rt.official = 'S'
  ORDER BY rt.created_stamp DESC 

