Why is this sub query bringing back NULL values when the table learner_employment contains no NULL values in the EMPLOYMENT_STATUS field? If I run it as as standalone query on any person code I get values. It seems to be an issue with the 'top 1' command, as there are values if I remove this. However I require only the earliest employment record.
select
p.PERSON_CODE,
EMPLOYMENT_STATUS,
EMPLOYMENT_INTENSITY,
DATE_STATUS_APPLIES
from people p
left join
(select top 1 PERSON_CODE,
EMPLOYMENT_STATUS,
EMPLOYMENT_INTENSITY,
DATE_STATUS_APPLIES
from learner_employment
order by DATE_STATUS_APPLIES) emp
on emp.PERSON_CODE = p.PERSON_CODE
1条答案
按热度按时间pcww981p1#
I suspect the problem is your misunderstanding of how the
JOIN
is working. I'm going to provide a MRE here, as your question lacks one. Firstly some sample data:Now lets write a query that represents what your does:
This returns the following dataset:
This is expected, the subquery returns one row and then that is
LEFT JOIN
ed onto. We can see what the subquery would return with the following:Which, unsurprisingly, returns the following:
This is because
SomeID
3
has the row with the highest value ofAnotherDate
and so in the priordata set onlySomeID
3
has a value inAnotherDate
(1
,2
, and4
aren't equal to3
so theLEFT JOIN
doesn't return a row).Perhaps what you want is instead of a
JOIN
is a correlated query. You need to useAPPLY
for this. As you have aLEFT JOIN
, then presumably you need anOUTER APPLY
. This would then return theTOP (1)
row for each correlation:Which returns the following:
Though if this is your goal,you should just use a
MAX
(not sure that's the case here mind):Alternatively, you can use the Get top 1 row of each group solution.