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 8390863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:06:53+00:00 2026-06-09T19:06:53+00:00

I have the following data table and data: mysql> describe school_data_sets_numeric_data; +————–+—————-+——+—–+———+—————-+ | Field

  • 0

I have the following data table and data:

mysql> describe school_data_sets_numeric_data;
+--------------+----------------+------+-----+---------+----------------+
| Field        | Type           | Null | Key | Default | Extra          |
+--------------+----------------+------+-----+---------+----------------+
| id           | int(11)        | NO   | PRI | NULL    | auto_increment |
| data_set_nid | int(11)        | NO   |     | NULL    |                |
| school_nid   | int(11)        | NO   |     | NULL    |                |
| year         | int(11)        | NO   |     | NULL    |                |
| description  | varchar(255)   | NO   |     | NULL    |                |
| value        | decimal(18,12) | NO   |     | NULL    |                |
+--------------+----------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

+----+--------------+------------+------+-------------------+------------------+
| id | data_set_nid | school_nid | year | description       | value            |
+----+--------------+------------+------+-------------------+------------------+
| 54 |        19951 |      19944 | 2008 | Asian             |  75.000000000000 |
| 51 |        19951 |      19944 | 2008 | White             | 200.000000000000 |
| 52 |        19951 |      19944 | 2008 | African American  | 100.000000000000 |
| 53 |        19951 |      19944 | 2008 | Hispanic          |  50.000000000000 |
| 55 |        19951 |      19944 | 2008 | Native American   |   9.000000000000 |

I want to display an average of the data instead of raw numbers. I want to do this in raw sql.

Example:

+----+--------------+------------+------+-------------------+------------------+
| id | data_set_nid | school_nid | year | description       | average          |
+----+--------------+------------+------+-------------------+------------------+
| 54 |        19951 |      19944 | 2008 | Asian             |  17.28%          |
| 51 |        19951 |      19944 | 2008 | White             | 46.06%           |
| 52 |        19951 |      19944 | 2008 | African American  | 23.04%           |
| 53 |        19951 |      19944 | 2008 | Hispanic          |  11.52%          |
| 55 |        19951 |      19944 | 2008 | Native American   |  2.07%           |

What is the best way to do this? Keep in mind there will be multiple data sets and years.

  • 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-06-09T19:06:54+00:00Added an answer on June 9, 2026 at 7:06 pm

    One way to do this is to write a query that gets the “total value”, which will be the denominator for your average.

    SELECT SUM(value) AS total_value FROM school_data_sets_numeric_data
    

    Join the results of that query with each row of the table, so you have the value and total available, which you use to calculate the average.

    SELECT v.id
         , v.data_set_nid
         , v.school_nid
         , v.year
         , v.description
    --   , v.value
    --   , t.total_value
         , CONCAT(FORMAT(IF(t.total_value=0,0,(v.value*100.0)/t.total_value),2),'%')
           AS `average`
      FROM (SELECT SUM(value) AS total_value FROM school_data_sets_numeric_data) t
     CROSS
      JOIN school_data_sets_numeric_data v
    

    The keyword CROSS is optional, but it serves as documentation to later reviewers, and lets them know that we intended to create a Cartesian product, and that we’ve purposefully left out (and not accidentally omitted) an ON clause for the JOIN.

    The expression that returns the “average”, displays the value as you show it… rounded to two decimal places with a trailing percent sign.

    Calculating the “average” is straightforward… value / total_value * 100.

    (Beware of the “integer” division if both numerator and demoninator are integer types, not an issue in your case, but we make sure numerator is decimal by multiplying by decimal literal, which insures we have decimal type value.)

    The rest of the expression deals with avoiding possible “divide by zero” exception, rounding to two decimal places, formatting two fixed decimal places, and appending the ‘%’.

    If you want those % signs to line up, you can pad the left side of that string with spaces, to a specific length, e.g. LPAD(expr,7,' '). (Actually, I would probably do that before the CONCAT that adds the %’, but it doesn’t really matter.)


    If you intend to do an average on a subset, for example, for a particular year and a particular dataset, instead of the entire return set, then we get the “subtotal” for each of those groups of data.

    Here, we’re calculating the average from a particular data_set_nid and year:

    SELECT v.id
         , v.data_set_nid
         , v.school_nid
         , v.year
         , v.description
    --   , v.value
    --   , t.total_value
         , CONCAT(FORMAT(IF(t.total_value=0,0,(v.value*100.0)/t.total_value),2),'%')
           AS `average`
      FROM (SELECT u.data_set_nid
                 , u.year
                 , SUM(u.value) AS total_value 
              FROM school_data_sets_numeric_data u
             GROUP
                BY u.data_set_nid
                 , u.year
           ) t
      JOIN school_data_sets_numeric_data v
        ON v.data_set_nid = t.data_set_nid
         , v.year = t.year
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
I have a table RDCAlerts with the following data in a column of type
I have the following table: CREATE TABLE [dbo].[Data] ( [Id] UNIQUEIDENTIFIER NOT NULL, [Data]
I have following data in a mysql table, +------------+--------+--------+ | date | inQty |
I have the following mysql table called test. lets say it has the data
I have a php variable: $foo My MySQL table called data has the following
So, I have got following data in a MySQL table: mysql> SELECT * FROM
So im having a problem (obviously). I have the following MySQL table data 7
I have a MySQL table with following data (sample); UserId - DeviceId - StartDate
I have the following MySql table containing my raw event data (about 1.5 million

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.