  
CREATE OR ALTER FUNCTION GetModifiedAfterValuesForInvestigationAPI(  
    @StartTime DATETIME2,  
    @EndTime DATETIME2  
)  
RETURNS TABLE  
AS  
RETURN  
(  
	SELECT        
	C.STARTTIME,        
	C.ENDTIME,      
	B.TYPNR_T AKTTYP_T,         
	Q.KLARTEXT AEVERKTYP_T,         
	Q.XNUM40 WORKFLOW_T,         
	O.NAMN ORG_T,         
	H.NAMN CASEWORKER,        
	A.NAMN AKTPNR_NAMN,        
	A.KUNDNUMMER,      
	C.STARTDAT,      
	C.STOPDAT,      
	C.AKTPNR,      
	C.AEVERKTYP,      
	c.AEVERKNR,      
	H.HAND,      
	C.MEDHAND,      
	I2.NAMN AS COCASEWORKER,      
	C.ORGID,      
	C.ORGENHNR,      
	C.FAMRTYP1,      
	C.FAMRTYP2,      
	C.FAMRTYP3,      
	T10.KLARTEXT STATGRP1_T,         
	T11.KLARTEXT STATGRP2_T,          
	T12.KLARTEXT STATGRP3_T,          
	T13.KLARTEXT STATGRP4_T,          
	T14.KLARTEXT STATGRP5_T,      
	STAT.XNUM6 AS STATISTICQUANTITYID,        
	STAT1.KLARTEXT AS STATISTICQUANTITY      
	FROM LD0P0C FOR SYSTEM_TIME CONTAINED IN (@StartTime, @EndTime) C     
	LEFT JOIN LD0V0A A ON C.AKTPNR = A.PNR   
	LEFT JOIN LD0VAAYB B ON C.AKTNR = B.AKTNR       
	LEFT JOIN LD0V0Q01T Q ON Q.KODTYP = 'UTREDTYP' AND Q.KOD = C.AEVERKTYP        
	LEFT JOIN LD0V0J01 O ON C.ORGID = O.ORGID AND C.ORGENHNR = O.ORGENHNR        
	LEFT JOIN LD0V0I H ON C.HAND = H.HAND      
	LEFT JOIN LD0V0I I2 ON C.MEDHAND = I2.HAND          
	LEFT OUTER JOIN LD0V0Q04 STATGRPVAL ON STATGRPVAL.KODTYP = 'UTREDTYP' AND C.AEVERKTYP = STATGRPVAL.KOD       
	LEFT OUTER JOIN LD0V0Q01T T10 ON T10.KODTYP = 'STATGRUPP' AND T10.XTXT1 = 'UTREDTYP' AND T10.KOD = STATGRPVAL.STATGRP1      
	LEFT OUTER JOIN LD0V0Q01T T11 ON T11.KODTYP = 'STATGRUPP' AND T11.XTXT1 = 'UTREDTYP' AND T11.KOD = STATGRPVAL.STATGRP2       
	LEFT OUTER JOIN LD0V0Q01T T12 ON T12.KODTYP = 'STATGRUPP' AND T12.XTXT1 = 'UTREDTYP' AND T12.KOD = STATGRPVAL.STATGRP3        
	LEFT OUTER JOIN LD0V0Q01T T13 ON T13.KODTYP = 'STATGRUPP' AND T13.XTXT1 = 'UTREDTYP' AND T13.KOD = STATGRPVAL.STATGRP4      
	LEFT OUTER JOIN LD0V0Q01T T14 ON T14.KODTYP = 'STATGRUPP' AND T14.XTXT1 = 'UTREDTYP' AND T14.KOD = STATGRPVAL.STATGRP5            
	LEFT OUTER JOIN LD0V0Q01T    AS STAT   ON STAT.KODTYP = 'UTREDTYP' AND STAT.KOD = C.AEVERKTYP      
	LEFT OUTER JOIN LD0V0Q01T    AS STAT1   ON STAT1.KOD = STAT.XNUM6 AND STAT1.KODTYP = 'SCBKODUTR'
    INNER JOIN LD0FGetMaxEndTimeForInvestigationAPI(@StartTime, @EndTime) M 
	ON C.AEVERKNR = M.AEVERKNR AND C.ENDTIME = M.MaxEndTime  
	WHERE C.TMARK = 0       
)  
GO