drop view LD0V1C60;

CREATE VIEW LD0V1C60 (BETALNR, MANREGNR, KLIARBLNR, PLACNR, FRVLNR, INSNR, 
	DATUM, BELOPP, FAMILJENR, INSTYP, FOMDAT, TOMDAT, 
	FRVFROMTID, FRVTOMTID, AKTPNR, ANTAL, HANDELSE, ENHET, 
	INSTITUTION, SERVICEPRODUKT, PLACFROM, PLACTOM, 
	OMF_PNR, PLAOMFFROM, PLAOMFTOM, FRVTYP )
AS
SELECT F.BETALNR, 0 "MANREGNR", 0 "KLIARBLNR", 0 "PLACNR", 0 "FRVLNR", F.INSNR, 
       F.BETDAT, F.BELOPP, A30.FAMILJENR, C20.INSTYP, C20.FOMDAT, '00000000' "TOMDAT",
       '00000000' "FRVFROMTID", '00000000' "FRVTOMTID", C20.AKTPNR, 0 "ANTAL", 0 "HANDELSE", 0 "ENHET",
       0 "INSTITUTION", 0 "SERVICEPRODUKT", '00000000' "PLACFROM", '99999999' "PLACTOM", 
       ' ' "OMF_PNR", '00000000' "PLAOMFFROM", '99999999' "PLAOMFTOM", 0 "FRVTYP"
 FROM LD0V0F F, LD0VLC20 C20, LD0V0A30 A30
  WHERE F.INSNR = C20.INSNR
        AND C20.BSKFAMLNR = A30.LNR
   	AND C20.BSKFAMLNR > 0
   	AND F.BETTYP < 90
   	AND F.BETSATT < 90
   	AND F.INSNR > 0
UNION
 SELECT 0, C60.MANREGNR, 0, 0, 0, C60.INSNR, 
        C60.DATUM, C60.BELOPP, A30.FAMILJENR, C20.INSTYP, C20.FOMDAT, '00000000',
        '00000000', '00000000', C20.AKTPNR, C60.ANTAL, C60.HANDELSE, C60.ENHET, 
        0, 0, '00000000', '99999999',
        ' ', '00000000', '99999999', 0
 FROM LD0V0C60 C60, LD0VLC20 C20, LD0V0A30 A30
  WHERE C60.INSNR = C20.INSNR
        AND C20.BSKFAMLNR = A30.LNR
   	AND C20.BSKFAMLNR > 0
UNION
 SELECT 0, 0, C30.KLIARBLNR, 0, 0, C30.INSNR, 
        C30.TILLFALLE, C30.ANTAL * C30.PRISKOSTN, A30.FAMILJENR, C20.INSTYP, C20.FOMDAT, '00000000',
        '00000000', '00000000', C20.AKTPNR, C30.ANTAL, C30.HANDELSE, 0, 
        0, 0, '00000000', '99999999', 
        ' ', '00000000', '99999999', 0
 FROM LD0V0C30 C30, LD0VLC20 C20, LD0V0A30 A30
  WHERE C30.INSNR = C20.INSNR
        AND C20.BSKFAMLNR = A30.LNR
   	AND C20.BSKFAMLNR > 0
UNION
 SELECT 0, 0, 0, L93.PLACNR, 0, C20.INSNR,
        '00000000', 0, A30.FAMILJENR, C20.INSTYP, C20.FOMDAT, '00000000',
        '00000000', '00000000', C20.AKTPNR, 0, 0, 0,
        L93.INSTNR, L93.SERVICEP, L93.FROMDAT, L93.TOMDAT, 
        L94.PNR, L94.FROMDAT, L94.TOMDAT, 0
 FROM LD0VYL93 L93, LD0V0L94 L94, LD0VLC20 C20, LD0VYE E, LD0V0A30 A30
  WHERE L94.PLACNR = L93.PLACNR 
        AND L93.BESLUTNR = E.BESLUTNR 
        AND E.INSNR = C20.INSNR 
        AND C20.BSKFAMLNR = A30.LNR
   	AND C20.BSKFAMLNR > 0
UNION
 SELECT 0, 0, 0, L81.PLACNR, L81.FRVLNR, 0,
        '00000000', 0, A30.FAMILJENR, C20.INSTYP, L81.FROMDAT, L81.TOMDAT,
        L81.FROMTID, L81.TOMTID, C20.AKTPNR, 0, 0, 0, 
        L93.INSTNR, L93.SERVICEP, L93.FROMDAT, L93.TOMDAT, 
        L94.PNR, L94.FROMDAT, L94.TOMDAT, L81.TYP
 FROM LD0V0L81 L81, LD0VYL93 L93, LD0V0L94 L94, LD0VLC20 C20, LD0VYE E, LD0V0A30 A30
  WHERE L81.PLACNR = L94.PLACNR 
        AND L94.PLACNR = L93.PLACNR 
        AND L93.BESLUTNR = E.BESLUTNR 
        AND E.INSNR = C20.INSNR 
        AND C20.BSKFAMLNR = A30.LNR
   	AND C20.BSKFAMLNR > 0 
        AND L94.PNR = L81.PNR
;
