Aggregatfunktionen:
Zugriff auf Subselect
-- Zu jedem Tank
soll das Buchungsdatum der ersten Lieferung angezeigt werden ...
-- ... und dazu
noch ein paar weitere Daten aus dieser Lieferung, das ist das Problem ...
SELECT tank
,p_objekt.getnr(GPI) kdnr
, (SELECT KURZNAME FROM SGGP gp WHERE gp.GPID = GPI) kdnanme
, rest
, ENTITAETSID
, GPI
, KONPOSID
, p_objekt.getnr(KONPOSID)
, dtBudatum
from (
-- ... dazu bildet man ein Subselect mit allen Lieferungen ...
SELECT
te.ENTITAETSNR tank
, kpvp.restfuellstand rest
, te.ENTITAETSID
, k.GPID gpi
, kpvp.KONPOSID
-- ... gibt für jeden Tank jeder Lieferung eine Zeilennummer
, row_number() over (partition by tank.SILOID order by kp.LIEFERDATUM) LAST_REC
, (select max(a.buchungsdatum) from abrech a, abrechpos ap, abrechposvk app, koposvk ko
where app.konposid = ko.konposid and ko.kontraktid = kp.kontraktid and ap.rechposid = app.rechposid
and ap.buchungsartid =:idJabuartid and ap.rechid = a.rechid and a.stornokz = ' ' and ap.checkli = ' ') dtBudatum
FROM
PASILO tank
, SVENTITAET te
, KOPOSVKPROD kpvp
, SPPROD sp
, KOPOSVK kp
, KOKONTRAKT k
, KOTRANSPORT kt
WHERE
te.ENTITAETSID = tank.SILOID
AND kpvp.SILOID = tank.SILOID
AND tank.siloid
> 0
AND tank.refsiloid = 0
AND tank.siloid not in (select refsiloid
from pasilo)
AND tank.siloid <= :dSiloid2
AND tank.siloid >= :dSiloid1
AND kpvp.konposid = kp.konposid
AND kpvp.prodid = sp.prodid
AND kp.kontraktid = k.kontraktid
AND kp.kontraktid = kt.kontraktid (+)
AND kt.spediteurid = 0
AND kpvp.menge1 = 0
AND trim(sp.zusatz1) is null
AND 1 = decode(:idGP,0, 1, decode(k.GPID,:idGP, 1, 0))
and not exists
(select 1 from ABRECH ar
, ABRECHPOS ap
, ABRECHPOSVK app
,SVENTITAET ape
, PAPARTIETEIL pt
WHERE ar.rechid
= ap.rechid
AND pt.rechposid = ap.rechposid
AND ar.BUCHUNGSKZID = :idBuchungsartid
AND ar.valutadatum <= :idtBudatum
AND ap.rechposid = app.rechposid
AND ar.gpid = k.gpid
AND ape.entitaetsid = ap.rechposid
AND ape.gesperrt <> 'V'
AND 'S' <> ap.checkli
AND ar.stornokz = ' '
AND (ap.buchungskz <> 'W ' or app.konposid = 0)
AND pt.partieid = tank.partieid
AND pt.buchungsartid = 0
)
-- ... und nimmt dann aus diesem großen Join nur die Datensätze
mit der ersten Lieferung.
-- Das hat den Vorteil, dass man vorher Zugriff auf Alles hat.
) where LAST_REC = 1
ORDER BY tank ASC;
No comments:
Post a Comment