I have following query
SELECT Cod ,
MIN(Id) AS id_Min,
MAX(Id) AS id_Max,
-- retrieve value in the middle,
COUNT(*) AS Tot
FROM Table a ( NOLOCK )
GROUP BY Cod
HAVING COUNT(*)=3
how could i retrieve value between min and max as i have done for min and max?
EXAMPLES
Cod | Id
Stack 10
Stack 15
Stack 11
Overflow 100
Overflow 120
Overflow 15
Required output
Cod | Min | Value_In_The_Middle | Max
Stack 10 11 15
Overflow 15 100 120
I haven’t tested this but I think this would work
This trick only works when you have 3 values and the SUM() doesn’t overflow (like Bogdan Sahlean points out in the comments).