CREATE OR ALTER VIEW LD0ApiPayment AS  
SELECT F.*,  
F.STARTTIME,  
F.ENDTIME,  
F10.BENAMN,
I.NAMN AS DECISIONMAKER,
Q01_1.KLARTEXT AS BETTYP_T,  
Q01.KLARTEXT AS BETSATT_T,  
(SELECT  DISTINCT KUNDNUMMER FROM LD0P0A A   
  WHERE EXISTS (SELECT * FROM LD0V0C20T C20 WHERE C20.AKTPNR=A.PNR   
   AND C20.INSNR=F.INSNR) ) AS KUNDNUMMER,  
(SELECT DISTINCT PNR FROM LD0P0A A   
  WHERE EXISTS (SELECT * FROM LD0V0C20T C20 WHERE C20.AKTPNR=A.PNR   
   AND C20.INSNR=F.INSNR) ) AS PNR  
  
FROM LD0P0F F  
LEFT JOIN LD0V0F10 F10 ON F10.EKENHNR = F.EKENHNR
LEFT JOIN LD0P0I I ON I.HAND = F.BESFATT
LEFT OUTER JOIN LD0V0Q01T Q01 ON Q01.KODTYP = 'BETALSATT' AND Q01.KOD = F.BETSATT   
LEFT OUTER JOIN LD0V0Q01T Q01_1 ON Q01_1.KODTYP = 'BETALTYP' AND Q01_1.KOD = F.BETTYP   
  
WHERE F.BETSATT <> 90   
AND F.STATUS <> 9  
AND F.BETTYP <> 90  
AND F.BETTYP <> 91  
GO
GRANT ALL ON LD0ApiPayment TO LD0ROANV
GO