Hi @Simon Edri ,
Welcome to the microsoft TSQL Q&A forum!
Please try:
Select
C.CID,C.NID,C.FirstName,C.LastName,C.ID,
PM.AccountNum,PM.Balnce,PM.DateFrom,PM.DateTo,PM.MoveSum,PM.OpenDate,PM.ExportDate,PM.Rem,
PT.PayTypeName,
P.PayID, P.OpenDate
From Customers C
LEFT JOIN PaymentsMoves PM ON C.CID = PM.CID
LEFT JOIN (SELECT * FROM (SELECT PayID , OpenDate, PayDate,CID, PaySum,
MAX(CID) OVER(PARTITION BY PayID) mm
FROM Payments) t
WHERE CID=mm) P
ON P.CID = C.CID AND PM.CID IS NULL
LEFT JOIN PayType PT ON PT.PID = PM.MoveType
If this does not solve your problem, please share us your table structure (CREATE TABLE …) and some sample data(INSERT INTO …)along with your expected result.So that we’ll get a right direction and make some test.
If you have any question, please feel free to let me know.
If the response is helpful, please click "Accept Answer" and upvote it.
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.