calcualte median in MySQL

I am  trying to determine the median of a column in a table.

given the following values from a  table column
AMOUNT
15.00
100.00
20.00
15.00
15.00
15.00
15.00
15.00
15.00
15.00
15.00

and the following formula/select statement

SELECT x.amount
FROM DONATIONS x , DONATIONS y
GROUP BY x.amount
HAVING SUM(SIGN(1-SIGN(y.amount-x.amount))) = (COUNT(*)+1)/2;

Open in new window

the results  should be  15
however i am getting null, removing the  having clause shows the following output
AMT,             SIGN, COUNT
15.00,      81,       50.0000
20.00,      10,       6.0000
100.00,      11,       6.0000

the actual output should be 15


i have a similar call that does it on  summed values that woirks peerfectly, but non-aggreate values are not working
SELECT   d.amount
FROM     (
                   SELECT    Sum(d.amount) AS amount
                   FROM      donations d
                   LEFT JOIN recipients r
                   ON        d.donation_id = r.donation_id
                   WHERE     donation_status = 'verified'
                   OR        donation_status = 'unverified'
                   GROUP BY  recipient_id) d,
         (
                   SELECT    sum(d.amount) AS amount
                   FROM      gr_donations d
                   LEFT JOIN grdp_recipients r
                   ON        d.donation_id = r.donation_id
                   WHERE     donation_status = 'verified'
                   OR        donation_status = 'unverified'
                   GROUP BY  recipient_id) dd
GROUP BY d.amount
HAVING   sum(sign(1-sign(dd.amount-d.amount))) = (count(*)+1)/2),0 

Open in new window



no idea why the non-aggretate is not working


TIA
LVL 25
James RodgersWeb Applications DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Mark BullockQA EngineerCommented:
This query worked for me:
SELECT x.amount 
FROM test.donations x, test.donations y
GROUP BY x.amount
HAVING SUM(SIGN(1-SIGN(y.amount-x.amount)))/COUNT(*) > .5
LIMIT 1;

Open in new window

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
James RodgersWeb Applications DeveloperAuthor Commented:
went in a different direction, but this was the base used, B because i already had the base query
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
MySQL Server

From novice to tech pro — start learning today.