Accessing variables in MySQL query

I'm not highly skilled in SQL, and my query isn't working, and I'd like to know what I'm doing wrong.  I'm trying to write a query in MySQL to calculate a median.  My logic is straightforward:

1) Calculate position of median value based on number of entries in table, call this number n.
2) Select the nth row of ordered values in table based on numbering rows with a variable

The first query that calculates the value 'n' works fine.  The second query will return all the values as row number and Lat number from the Station table when I set the Where clause to be 'Where 1' instead of what's written below.

set @n = (SELECT Truncate(Count(S.Lat_N)/2,0) as N FROM STation AS S);
SELECT @rownum:=@rownum+1 as `row_number`, S.Lat_N
  FROM Station S,  (SELECT @rownum:=0) r
--WHERE 1
  WHERE @rownum = @n
  ORDER BY S.Lat_N;

Open in new window


The table name is Station and I'm focused on the column named Lat_N.

When I expand the Where clause as written, I get no results.  In some variations it complains it doesn't know what rownum is or what n or @n are either, but I'm just trying to figure out this first part.

What would be the proper way to accomplish the task of selecting the nth row as I'm trying to do above?
LVL 11
ugebAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
You need to use a subquery for this:

set @n = (SELECT Truncate(Count(S.Lat_N)/2,0) as N FROM STation AS S);
SELECT * FROM(
SELECT @rownum:=@rownum+1 as `row_number`, S.Lat_N
  FROM Station S,  (SELECT @rownum:=0) r
--WHERE 1
  ORDER BY S.Lat_N) as a
  WHERE row_number = @n;

Open in new window

0
ugebAuthor Commented:
Great, thank you.  Can you explain why the subquery is necessary and why my original query didn't work?
0
chaauCommented:
I think, when you try accessing the @rownum variable from the where clause it gets it value as it is initialised in the inline subquery (SELECT @rownum:=0) r resulting in it equal to 0 for every row. When you use the subquery as per my example you are using the row_number column which is correctly evaluated to the current row number
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ugebAuthor Commented:
Great, thank you for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.