previously I opened an issue asking how to take the first row in a set of records which were resolved brilliantly. Pitifully my customer changed his request a little after so this time I'm asking your help to find the middle row in a set of records i.e. having this:
TemperatureID | CastingID | TemperatureDateTime | TemperatureValue |
---|---|---|---|
1421294 | 1073513 | 2021-01-07 11:53:00.000 | 1648 |
1421295 | 1073513 | 2021-01-07 11:54:00.000 | 1698 |
1421296 | 1073513 | 2021-01-07 11:57:00.000 | 1699 |
I have to take the 1421295 record, whereas having an even number either the middle + 1 or middle - 1
So starting from this query, which return the 1421294 record to be clear
SELECT *
FROM (
SELECT
CastingID, TemperatureValue, TemperatureDateTime
, ROW_NUMBER() OVER (PARTITION BY CastingID ORDER BY CastingID) RN
FROM Production.Temperatures
where YEAR(TemperatureDateTime) = 2021 and PhaseID = 250
) A
WHERE RN = 1 and year(A.TemperatureDateTime) = 2021
can I obtain the result I want or I have to change my approach?