Complex Subqueries

* find all the personnel areas/subareas belonging to campus
select werks btrtl btext into table t_t001p
from t001p
where werks like 'C%'.
loop at t_t001p. " loop through campus personnel areas/subareas
* select all the open positions for each personnel area/subarea
* the position must belong to an organization but must not have any
* people assigned to the position
select objid persa btrtl into table t_hrp1008
from hrp1008 as h1
where plvar = '01' " current plan
and otype = 'S' " positions
and subty = ' ' " subtype
and istat = '1' " active
and begda <= sy-datum " beginning on or before now
and endda >= sy-datum " ending on or after today
and persa = t_t001p-werks " personnel area
and btrtl = t_t001p-btrtl " personnel subarea
and exists ( select * " belonging to org
from hrp1001 as h2
where plvar = '01' " currnt plan
and otype = 'S' " position
and h2~objid = h1~objid " position #
and rsign = 'A'
and relat = '003' " belongs
and istat = '1' " active
and sclas = 'O' ) " organizatn
and not exists ( select * " holder of position
from hrp1001 as h3 " not found
where plvar = '01' " current pln
and otype = 'S' " position
and h3~objid = h1~objid " position #
and rsign = 'A'
and relat = '008' " held by
and istat = '1' " active
and sclas = 'P' ). " person
clear w_subtotal.
loop at t_hrp1008.
w_subtotal = w_subtotal + 1.
w_total = w_total + 1.
write: / t_hrp1008-objid, t_hrp1008-persa, t_hrp1008-btrtl,
t_t001p-btext.
endloop.
write: / 'Total available for', t_t001p-btext, w_subtotal.
uline.
skip 2.
endloop.
write: / 'Grand Total available:', w_total.