CREATE OR ALTER VIEW LD0ApiAktualisering      
AS      
SELECT H.*,         
H.STARTTIME,         
H.ENDTIME,         
T5.KLARTEXT AKTUALTYP_T,        
T.KLARTEXT AKTUAL_T,        
T3.KLARTEXT AVEM_T,        
T2.KLARTEXT AORSAK_T,        
I.NAMN CASEWORKER,        
I2.NAMN COCASEWORKER,        
T4.NAMN ORGNAMN,        
P.NAMN,        
P.ADRESS,        
P.COADR,        
P.ORT,         
P.POSTNR,        
P.HEMTEL,        
P.ARBTEL,        
P.MOBIL,        
P.EPOSTHEM,        
P.FODDAT,        
P.KON,        
P.KUNDNUMMER,   
T1.KLARTEXT DOSSIER,
T6.KLARTEXT AKTUAVSER_T,        
AKTS.STATUS AKTUALSTATUS,        
STATGRPVAL.STATGRP1,         
STATGRPVAL.STATGRP2,        
STATGRPVAL.STATGRP3,         
STATGRPVAL.STATGRP4,        
STATGRPVAL.STATGRP5,         
T10.KLARTEXT STATGRP1_T,        
T11.KLARTEXT STATGRP2_T,         
T12.KLARTEXT STATGRP3_T,        
T13.KLARTEXT STATGRP4_T,         
T14.KLARTEXT STATGRP5_T        
        
FROM LD0P0H H         
LEFT JOIN LD0V0A P         
ON H.PNR = P.PNR         
LEFT OUTER JOIN LD0V0I I         
ON I.HAND = H.HAND
LEFT JOIN LD0VYB B  
ON B.AKTNR = H.AKTNR  
LEFT OUTER JOIN LD0V0Q01T T1          
ON T1.KODTYP = 'AKTTYP' AND T1.KOD = B.TYPNR          
LEFT OUTER JOIN LD0V0Q01T T         
ON T.KODTYP = 'AKTUAL' AND T.KOD = H.AKTUAL         
LEFT OUTER JOIN LD0V0Q01T T5        
ON T5.KOD = H.AKTUALTYP AND T5.KODTYP = 'AKTUALTYP'         
LEFT OUTER JOIN LD0V0Q01T T2         
ON T2.KODTYP = 'AKTUORSAK' AND T2.KOD = H.AORSAK         
LEFT OUTER JOIN LD0V0Q01T T3         
ON T3.KOD = H.AVEM AND T3.KODTYP = 'AKTUFRÅN'         
LEFT OUTER JOIN LD0V0J01 T4         
ON T4.ORGID = H.ORGID AND T4.ORGENHNR = H.ORGENHNR         
LEFT OUTER JOIN LD0V0I I2         
ON I2.HAND = H.HAND2         
LEFT OUTER JOIN LD0V0Q01T T6        
ON T6.KOD = H.AVSER AND T6.KODTYP = 'AKTUAVSER'         
LEFT OUTER JOIN LD0V0AAktualiseringStatus AKTS        
ON AKTS.AKTUALNR = H.AKTUALNR        
LEFT OUTER JOIN LD0V0Q04 STATGRPVAL         
ON STATGRPVAL.KODTYP = 'AKTUAL'         
AND H.AKTUAL = STATGRPVAL.KOD         
LEFT OUTER JOIN LD0V0Q01T T10           
ON T10.KODTYP = 'STATGRUPP'           
AND T10.XTXT1 = 'AKTUAL'           
AND T10.KOD = STATGRPVAL.STATGRP1           
LEFT OUTER JOIN LD0V0Q01T T11           
ON T11.KODTYP = 'STATGRUPP'           
AND T11.XTXT1 = 'AKTUAL'           
AND T11.KOD = STATGRPVAL.STATGRP2           
LEFT OUTER JOIN LD0V0Q01T T12           
ON T12.KODTYP = 'STATGRUPP'           
AND T12.XTXT1 = 'AKTUAL'           
AND T12.KOD = STATGRPVAL.STATGRP3           
LEFT OUTER JOIN LD0V0Q01T T13           
ON T13.KODTYP = 'STATGRUPP'           
AND T13.XTXT1 = 'AKTUAL'           
AND T13.KOD = STATGRPVAL.STATGRP4           
LEFT OUTER JOIN LD0V0Q01T T14           
ON T14.KODTYP = 'STATGRUPP'           
AND T14.XTXT1 = 'AKTUAL'           
AND T14.KOD = STATGRPVAL.STATGRP5

GO
GRANT ALL ON LD0ApiAktualisering TO LD0ROANV
GO