IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0CAKTUALSERING')
BEGIN
DROP VIEW LD0V0CAKTUALSERING
END
;
CREATE VIEW LD0V0CAKTUALSERING
AS 
SELECT
	H.AKTUALNR, H.ORGID, H.AVBORSAK AVBORSAK, H.AKTUAL, H.AKTUALTYP, H.BTYP, H.ADATUM, H.AVEM, H.AORSAK, 
	H.ORGENHNR, H.ORGENHNR2, H.PNR, H.HAND, H.HAND2, H.ATEXT1, H.ATEXT2, H.ATEXT3, H.ATEXT4, 	
	H.AVBFROMDAT, H.AVBTOMDAT, H.REGTID, H.AVSER, H.UPDDAT, H.UPDSIGN, H.PROCDATUM, H.NEXTMONTH,
	H.URGENCY, H.URSPRUNG, H.RESPOND_DATE, H.AEVERKNR, H.INSNR, H.CASEID,
	H.AKTTYP, C.STARTDAT INVESTIGATIONSTARTDATE, C.STOPDAT INVESTIGATIONSTOPDATE, C.AEVERKTYP INVESTIGATIONTYPE,
	C20.FOMDAT SERVICESTARTDATE, C20.INSTYP SERVICETYPE,
	P.NAMN PERSONNAME, P.KUNDNUMMER PERSONKEY, 
	T.KLARTEXT TYPECODETEXT,T.XNUM1 AKTUALSERINGTYPE,IIF(H.AKTUALTYP = 1,T.XNUM6,T.XNUM5) AS DEFAULTHANDLINGTIME, IIF(H.AKTUALTYP = 1, ISNULL(T.XNUM10,0), ISNULL(T.XNUM8,0)) AS USEWORKINGDAYS, ISNULL(T.XNUM17,0) SHOWINLIFECARE,
	J01.NAMN ORGANIZATIONNAME, J02.NAMN COORGANIZATIONNAME

FROM LD0V0H H

INNER JOIN LD0P0A	P   ON H.PNR = P.PNR
LEFT  JOIN LD0VLC	C   ON H.AEVERKNR = C.AEVERKNR
LEFT  JOIN LD0VLC20 C20 ON H.INSNR = C20.INSNR
LEFT  JOIN LD0V0J01 J01 ON H.ORGID = J01.ORGID AND H.ORGENHNR = J01.ORGENHNR 
LEFT  JOIN LD0V0J01 J02 ON H.ORGID = J02.ORGID AND H.ORGENHNR2 = J02.ORGENHNR
LEFT  JOIN LD0V0Q01 T   ON  T.KODTYP = 'AKTUAL' AND T.KOD = H.AKTUAL
WHERE H.BESLUTNR = 0
;