How to use subquery in OFFSET in MySQL?

74 views Asked by At

I have been trying to solve this problem : https://www.hackerrank.com/challenges/weather-observation-station-20/problem?isFullScreen=true

It states to find the median, but everytime I am ending up with syntax error or runtime error.

SELECT ROUND(LAT_N, 4)
FROM STATION
ORDER BY LAT_N
LIMIT 1 OFFSET (
  SELECT 
    CASE
      WHEN COUNT(LAT_N) % 2 <> 0 THEN COUNT(LAT_N) / 2
      ELSE (COUNT(LAT_N) - 1) / 2
    END
  FROM STATION
);

This is the solution that I have worked upon. Any help is highly appreciated.

1

There are 1 answers

0
NAMIT DUBEY On
SELECT ROUND(s.LAT_N, 4) 
FROM STATION s 
WHERE (SELECT COUNT(LAT_N) FROM STATION WHERE s.LAT_N > LAT_N) = 
      (SELECT COUNT(LAT_N) FROM STATION WHERE s.LAT_N < LAT_N)