I’m trying to get info for the next most recent entry based on the ID and location and add that to the query I am running. Not the second most recent entry in the table, only the previous entry based on the ID.
I’m not sure if this is possible or I just can’t figure out how to do it or even which direction I should go.
Here is how the previous programmer did it:
SELECT -- blah blah blah
, (SELECT TOP 1 CAST(id AS VARCHAR) + '~' + CONVERT(VARCHAR, creation_date, 120)
FROM header hdr2
WHERE hdr2.id < hdr.id
AND hdr2.location = hdr.location
ORDER BY hdr2.id DESC) AS "prev_info"
FROM header hdr
/*blah blah blah*/
I understand why he wanted to use only a single sub query and tried to put everything into that one column, but I’m trying to optimize this and I would like to do it in some other,easier and more efficient, way.
I feel like the answer is staring me in the face, but I just can’t reach out and grasp it. If I need to explain myself any more, please don’t hesitate to ask.
Consider using
outer apply: