DROP VIEW LD0VAA0ServiceOccurrence
;

CREATE VIEW LD0VAA0ServiceOccurrence
AS
SELECT
	C20.INSNR,
	C20.AKTPNR,
	A.NAMN AKTPNR_NAMN,
	A.KUNDNUMMER,
	C20.FOMDAT,
	C20.TOMDAT,
	Q.KLARTEXT,
	C20.ORGENHNR,
	O.NAMN AS 'ORG_NAME',	
	H.NAMN HAND_NAMN,
	H.TELE1,
  C20.MEDHAND,
  MH.NAMN MEDHAND_NAMN
FROM LD0VL1C20 C20
	INNER JOIN LD0V0Q01 Q ON Q.KODTYP = 'INSATS' AND Q.KOD = C20.INSTYP 
	LEFT JOIN LD0V0I H ON C20.HAND = H.HAND 
    LEFT JOIN LD0V0I MH ON C20.MEDHAND = MH.HAND 
	LEFT JOIN LD0V0J01 O ON C20.ORGID = O.ORGID AND C20.ORGENHNR = O.ORGENHNR 
	LEFT JOIN LD0V0A A ON C20.AKTPNR = A.PNR
;