-- #New: 03.07.2020/S.Gueth
-- #Hint: on Update, please also update IR_WhereUsed.sql
--  #Doc: Param ir_q.puid = :ir_puid
SELECT
   Count(*) AS WhereUsedCount
from (
SELECT distinct
 /*MsSQL TOP (:top_count) MsSQL*/
 -- Count(*) AS WhereUsedCount
 irUsed.puid
FROM
 infodba.PITEMREVISION    ir_q
 INNER JOIN infodba.PITEM i_q ON (i_q.puid = ir_q.ritems_tagu)
 INNER JOIN infodba.PPSOCCURRENCE bo ON (   bo.rchild_itemu = ir_q.puid -- PRECISE
                                         or bo.rchild_itemu = ir_q.ritems_tagu -- IMPRECISE
                                        )
 INNER JOIN infodba.PPSBOMVIEWREVISION bvr ON (bvr.puid = bo.rparent_bvru)
 INNER JOIN infodba.PSTRUCTURE_REVISIONS sr on (sr.pvalu_0 = bvr.puid)
 INNER JOIN infodba.PITEMREVISION ir ON (ir.puid = sr.puid)     -- SELECT ir.pitem_revision_id AS IrId, ir.puid AS IrPuid
 INNER JOIN infodba.PITEM i ON (i.puid = ir.ritems_tagu)                -- SELECT i.pitem_id AS ItemId
 INNER JOIN infodba.PITEMREVISION irUsed ON (    (bvr.pis_precise = 1 AND irUsed.puid = ir.puid)  -- PRECISE only
                                              or (bvr.pis_precise = 0 AND irUsed.ritems_tagu = i.puid)   -- IMPRECISE alle IR of Item
                                              )
WHERE
 ir_q.puid = :ir_puid
 /*Ora AND ROWNUM <= :top_count Ora*/
 ) TBL


