我有两张桌子Person
和Salary
.
Person
:
PersonId | Name | Surname
--------------------------------
1 John Deer
2 Mark Bear
Salary
:
SId | PersonId | Date | Salary
----------------------------------------------------
1 2 2013-01-01 00:00:00.000 100
2 2 2012-01-01 00:00:00.000 90
3 2 2011-01-01 00:00:00.000 80
我想做的是,如果一个人有工资记录,那么它应该在结果中显示最新的工资信息,如果没有工资记录,那么它应该将工资信息显示为空,就像......
Result
------------------------------------------------------------------------
PersonId | Name | Surname | Date | Salary
1 John Deer NULL NULL
2 Mark Bear 2013-01-01 00:00:00.000 100
我知道它必须是这样的,但由于缺乏知识,我无法实现..
SELECT
P.PersonId, P.Name, P.Surname, SL.Date, SL.Salary
FROM
PERSON P
LEFT OUTER JOIN
(SELECT TOP 1 S.PersonId, S.Date, S.Salary
FROM Salary
WHERE S.PersonId = P.PersonId ORDER BY Date DESC) SL