Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7174935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:10:10+00:00 2026-05-28T16:10:10+00:00

I need to calculate the median value in MySQL. I saw the solution here

  • 0

I need to calculate the median value in MySQL. I saw the solution here.

But, I didn’t understand part of it. The solution provided enter code here is as follows:

SELECT x.val from data x, data y
GROUP BY x.val
HAVING SUM(SIGN(1-SIGN(y.val-x.val))) = (COUNT(*)+1)/2

What is data x and data y in the context of the original question? Usually FROM is followed by the table name. But, then why are 2 tables listed when the question refers to only one? Can someone explain how this solution works? Also, I didn’t understand this part: HAVING SUM(SIGN(1-SIGN(y.val-x.val))) .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:10:11+00:00Added an answer on May 28, 2026 at 4:10 pm

    What is data x and data y in the context of the original question?
    Usually FROM is followed by the table name. But, then why are 2 tables
    listed when the question refers to only one?

    In the original question, data x, data y joins the table to itself, creating a cartesian product. The original table had 7 rows, and by joining every row against every other row, the resulting product is 49 rows.

    Also, I didn’t understand this part: HAVING
    SUM(SIGN(1-SIGN(y.val-x.val)))
    .

    Essentially, this function determines for every value how many values are less than the one being examined. It then compares this total to half the count + 1… and then selects that value as the median.

    It does this by subtracting the value (x.val) from the value it is comparing (y.val). It then uses the SIGN function to convert the result to -1, 0, or 1. It then subtracts this value, and then takes the SIGN again. So if the y.val value is less than the x.val value it that is being compared to, the end result would be a 1. For example, let’s say y is 3, and x is 5.

    3 - 5 = -2
    SIGN(-2) = -1
    1 - (-1) = 2
    SIGN(2) = 1
    

    If y were 5, and x was 3… the end result would be 0:

    5 - 3 = 2
    SIGN(2) = 1
    1 - 1 = 0
    SIGN(0) = 0
    

    Summing the results of these comparisons gives us a number that indicates how many values come before the value that we’re examining. It then compares this SUM against COUNT(*) + 1 / 2 to find the middle range…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need calculate a thing. but my formula sentence has occur some problem. TextView
I need to calculate date difference using flowing query but it does not work.
I need to calculate a median on a set of data, so I created
I need to calculate the time passed between two dates. The catch here is
I need to calculate how much time was spent on a bug. I saw
I am new to SQL server. I need to calculate the median of the
I'm trying to calculate the median of a set of values, but I don't
I need calculate an URL and redirect to it. I have the following code:
Need to calculate IP => int just like in here: https://bitbucket.org/lorien/django-ipgeo/src/d19648c9b14f/ipgeo/models.py but in ruby.
I need to calculate the Italian fiscal code (tax code) in C#. I've found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.