Joining record with the most recent record on another table

前端 未结 1 1643
Happy的楠姐
Happy的楠姐 2021-01-28 11:04

I\'m trying to create a SQL view. How could I select the most recent record from one table leaving others as is. I need to select all records from all tables, which works fine,

1条回答
  •  轮回少年
    2021-01-28 12:00

    I would suggest apply:

    SELECT . . .
    FROM ptsweb.tblCustomerInqry i LEFT OUTER JOIN
         ptsweb.tblCustomer tc
         ON tc.CustomerID = i.CustomerID OUTER APPLY
         (SELECT TOP (1) p.*
          FROM ptsweb.tblProposal p
          WHERE tc.CustomerID = p.CustomerID 
          ORDER BY p.DateCreated DESC
         ) p
    WHERE i.CustInqDate > '2017-01-01' AND tc.CustomerID = 101568;
    

    0 讨论(0)
提交回复
热议问题