Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to SELECT based on value of another SELECT

Tags:

mysql

I have a table that looks something like this:

Name    Year   Value  A      2000     5  A      2001     3  A      2002     7  A      2003     1  B      2000     6  B      2001     1  B      2002     8  B      2003     2 

The user can query based on a range of years, and it will return the sum of Value grouped by Name, as such (assume queried years are 2000 - 2001):

Name   SUM(Value)  A         8  B         7 

Now, I wanted to insert a calculated field that outputs the ratio of the sum of the values of each name for ALL years to the sum of all values. Basically the percentage of all values attributed to A and B, respectively, like:

Name   SUM(Value)   % Of Total  A         8            0.484      (16 / 33)  B         7            0.516      (17 / 33) 

Note that even though the user queried only 2000-2001, I want the calculation to use the sum across all years. I've been searching and experimenting for hours and I cannot figure out how. I only know how to sum across the queried years like so:

SELECT `Name`, SUM(`Value`)/(SELECT SUM(`Value`) FROM `table1`) AS "% of Total" FROM `table1` WHERE `Year` BETWEEN 2000 AND 2001 GROUP BY `Name`; 
like image 206
John Avatar asked Apr 02 '12 03:04

John


People also ask

Can we use select inside select in MySQL?

In MySQL subquery can be nested inside a SELECT, INSERT, UPDATE, DELETE, SET, or DO statement or inside another subquery. A subquery is usually added within the WHERE Clause of another SQL SELECT statement.

Can we use select in where clause?

The WHERE clause can be used with SQL statements like INSERT, UPDATE, SELECT, and DELETE to filter records and perform various operations on the data. We looked at how to query data from a database using the SELECT statement in the previous tutorial.

What does select * from do?

An asterisk (" * ") can be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include: The FROM clause, which indicates the table(s) to retrieve data from.

Can you have multiple select statements?

To combine two or more SELECT statements to form a single result table, use the set operators: UNION, EXCEPT or INTERSECT. For example, assume that you have the following tables to manage stock at two book stores.


1 Answers

You can calculate the total (and from that the desired percentage) by using a subquery in the FROM clause:

SELECT Name,        SUM(Value) AS "SUM(VALUE)",        SUM(Value) / totals.total AS "% of Total" FROM   table1,        (            SELECT Name,                   SUM(Value) AS total            FROM   table1            GROUP BY Name        ) AS totals WHERE  table1.Name = totals.Name AND    Year BETWEEN 2000 AND 2001 GROUP BY Name; 

Note that the subquery does not have the WHERE clause filtering the years.

like image 114
kad81 Avatar answered Sep 22 '22 22:09

kad81