SELECT DISTINCT ISNULL(I.InvoiceNumber,'YOK') AS FaturaNo ,I.InvoiceHeaderID ,I.InvoiceLineID ,DENSE_RANK() OVER(PARTITION BY I.InvoiceHeaderID ORDER BY InvoiceLineID) AS KALEM_SAYISI , R.RowGuid ,R.TableName FROM AllInvoices I INNER JOIN [dbr].[DBA_CDClog] R ON R.RowGuid=InvoiceHeaderID WHERE R.Status='P'