IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0VAAYLMActivityPresenter')
BEGIN
DROP VIEW LD0VAAYLMActivityPresenter
END
;

CREATE VIEW LD0VAAYLMActivityPresenter AS 
SELECT U34.PLANAKTIVITETNR,
U34.INTROARB, 
U34.AKTIVTYP, 
U34.AKTIVITET, 
U34.INTANORD, 
U34.ANMALDAT, 
U34.PLANSTART, 
U34.PLANSLUT, 
U34.VERKSTART, 
U34.VERKSLUT, 
U34.SIGN,
U34.UPPFOLJNING, 
U34.RESULTAT, 
U34.UPDDAT, 
U34.UPDSIGN, 
U34.ENKELPLATSNR, 
U34.OVRIGPLATS, 
U34.OVRANT, 
U34.OVRIG1, 
U34.OVRIG2, 
U34.OVRIG3,
U34.OVRDAT, 
U35.STATUS,
U10.INSKRIVNINGSNR, 
U10.PERSONNR, 
U10.NAMN, 
U10.ADRESS, 
U10.COADR, 
U10.POSTNR, 
U10.ORT,  
U10.ORGID U10ORGID, 
U10.ORGENHNR U10ORGENHNR,
U10.FODLAND,
U10.KON,U10.INLEDDAT, 
U10.AVSLUTDAT, 
U30.SLUTLIG,  
U30.PLANNR, 
U30.VERSIONNR, 
U30.FROMDAT AS U30FROMDAT, 
U30.TOMDAT AS U30TOMDAT, 
U34.GRUPPNR,
U10.TYPAVSLUT AS U10TYPAVSLUT, 
U10.AVSLUTORS AS U10AVSLUTORS,
U34.HAND,
U70.BENAMNING AS ENKELPLATS_T,
U60.BENAMNING AS PRAKPROJ_T,
I.NAMN	AS HANDLAGGARE_T,
Q1.KLARTEXT AS AKTIVITET_T,
Q2.KLARTEXT AS AKTIVTYP_T,
Q3.KLARTEXT AS RESULTAT_T,
Q4.KLARTEXT AS INTANORD_T,
Q5.KLARTEXT AS OVRIGPLATS_T,
Q6.KLARTEXT AS OVRIG1_T,
Q7.KLARTEXT AS OVRIG2_T,
Q8.KLARTEXT AS OVRIG3_T,
Q9.KLARTEXT AS STATUS_T
FROM LD0VY1U10 U10 
JOIN LD0VYU30 U30 
ON U30.INSKRIVNINGSNR=U10.INSKRIVNINGSNR 
JOIN LD0VYU34 U34 
ON U34.PLANNR=U30.PLANNR 
AND U34.VERSIONNR=U30.VERSIONNR 
AND U34.VERSIONNR = (SELECT MAX(VERSIONNR) FROM LD0VYU34 U34B WHERE U34B.PLANNR = U30.PLANNR)
LEFT OUTER JOIN LD0V0U35 AS U35 ON U35.PLANAKTIVITETNR = U34.PLANAKTIVITETNR
LEFT OUTER JOIN LD0V0Q01 AS Q1 ON Q1.KODTYP = 'LNAKTIV' AND U34.AKTIVITET = Q1.KOD
LEFT OUTER JOIN LD0V0Q01 AS Q2 ON Q2.KODTYP = 'LNAKTIVTYP' AND U34.AKTIVTYP = Q2.KOD
LEFT OUTER JOIN LD0V0Q01 AS Q3 ON Q3.KODTYP = 'LNRESAKT' AND U34.RESULTAT = Q3.KOD
LEFT OUTER JOIN LD0V0Q01 AS Q4 ON Q4.KODTYP = 'LNINTANORD' AND U34.INTANORD = Q4.KOD
LEFT OUTER JOIN LD0V0Q01 AS Q5 ON Q5.KODTYP = 'LNOVRPLATS' AND U34.OVRIGPLATS = Q5.KOD
LEFT OUTER JOIN LD0V0Q01 AS Q6 ON Q6.KODTYP = 'LNOVRIG1' AND U34.OVRIG1 = Q6.KOD
LEFT OUTER JOIN LD0V0Q01 AS Q7 ON Q7.KODTYP = 'LNOVRIG2' AND U34.OVRIG2 = Q7.KOD
LEFT OUTER JOIN LD0V0Q01 AS Q8 ON Q8.KODTYP = 'LNOVRIG3' AND U34.OVRIG3 = Q8.KOD
LEFT OUTER JOIN LD0V0Q01 AS Q9 ON Q9.KODTYP = 'LNSTATUS' AND U35.STATUS = Q9.KOD AND U34.INTROARB = 2
LEFT OUTER JOIN LD0V0I AS I ON I.HAND = U34.HAND
LEFT OUTER JOIN LD0V0U70 AS U70 ON U70.ENKELPLATSNR = U34.ENKELPLATSNR
LEFT OUTER JOIN LD0V0U60 AS U60 ON U60.PRAKPROJNR = U35.PRAKPROJNR
;

