I have a table which has 3 columns (name, course, grade).
My 'name' column contains three students.
I need to sum grades of every student and divide it by the amount of grades.
I'm trying to do something like this,
SELECT
name
FROM
table
GROUP BY
name
AND
(SELECT SUM(grade) FROM table / SELECT COUNT(grade) FROM table GROUP BY name)
I'm not a native english speaker, so I hope you understand me.
I expect you want to calculate the average grade?
SELECT name, AVG(grade)
FROM `table`
GROUP BY name
your table design does not follow the 1st rule of normalization: atomicity. You have multiple names in single column AND you want to process them separately. This makes querying more difficult and inefficient. You need to normalize your table.
Related
Why do I not get the same results when running the two queries? If I run the second one I get the course with the smallest amount of credits and when I run the first one I get the courses ordered by courseid
select min(credits), title, courseid
from course
group by title, courseid
select min(credits)
from course
An aggregation query is any query that has a group by or an aggregation function in the select.
An aggregation query returns one row per group, where a "group" is defined as the unique combination of values of the keys in the group by clause. If there is no group by clause, then all rows are taken to be a single group and one row is returned.
So, your first query returns one row for each combination of title and courseid in the course table. That row contains the minimum value of credits for that combination. If the course table has only one row per courseid, then the results are very similar to the contents of the table.
The second query returns one row overall, with the minimum number of credits of all rows.
If you want to get one row from with the minimum number of credits, then you don't want an aggregation query. Instead, you can use:
select c.*
from course c
order by c.credits
limit 1;
When you use a group by, you are using a sort of "filter", in the first query you group by title, then all the same titles are grouped by courseid, in the second you only select the minimum value of credits without filtering.
Take a look at a group by doc maybe with some graphical examples like this:
https://www.geeksforgeeks.org/sql-group-by/
in the below image I'm using
SELECT DISTINCT(name),date,reporting,leaving from attendance where date='2016-09-01
and I'm still getting repeating names. Why?
When using DISCTINCT, MySQL uses all columns as grouping factor. If you want group by only one column and get all corresponding column values, use GROUP BY instead
SELECT name, date, reporting, leaving FROM attendance GROUP BY name WHERE ...
Actually your all rows have distinct data apart from Name column if you want only distinct names then you can get it with help of Aggregate functions, you can use MIN or MAX as per your business requirement
SELECT Name,MAX(date),MAX(reporting),MAX(leaving)
FROM attendance
WHERE date='2016-09-01'
GROUP BY Name
This might be the first time I've ever had to ask a question! Usually find things already answered! anyhoo... here goes...
recordset has a job_id column, there might be 20 rows for that job_id, a numerical value posted in another column for each row. I'd like to be able to query the table where I can sum together all the numbers for each job_id. the resultant recordset will have however many rows, one row for each job_id, and a column with all the numerical values totalled for that job_id (hope that makes sense!)
You can use GROUP BY to group results by job_id and make the sum of numeric values for each group.
select job_id, sum(numeric_val) sum_numeric_val
from mytable
group by job_id
try this query
SELECT job_id,SUM(numerical value)
FROM table_name
group by job_id
hope this help you
best regards
If I have a database of first names and annual incomes for two years as enumerated columns (I know this is poor database design but I don't have the liberty to change it at the moment). For example:
Name_2000, Name_2010, Income_2000, Income_2010
How can I construct a query to return all the unique names in both name columns in the first result column. The second result column should be the sum of all incomes for that name for 2000. The third should be the sum of all incomes for that name for 2010.
Obviously, in this example a person (record) may change names between years or not have a name in either year too.
Can I do this in a single query? The ability to filter by a certain subset of names would also be handy..
I would suggest using a sub query that unions 2 results together, one for each set of names and incomes, but each returning a dummy 0 for the income for the other year.
Something like this:-
SELECT aname, SUM(aincome2000), SUM(aincome2010)
FROM
(
SELECT Name_2000 AS aname, Income_2000 AS aincome2000, 0 AS aincome2010
FROM sometable
UNION ALL
SELECT Name_2010 AS aname, 0 AS aincome2000, Income_2010 AS aincome2010
FROM sometable
) sub1
GROUP BY aname
Suppose I have a table with a field named 'rating', it may take different values, but I want to receive a count of specific values.
Example:
Create table mytable(
rating int(1),
);
First and the obvious way I could think of was the following:
select rating,count(rating) from mytable group by rating order by rating
The problem though it is not clear how many values it would return, it may be also not easy to process them way.
What I would really like to do is to select two fields in one row showing the number of records that have some specific values.
Example...
//something like this (some pseudocode):
select count(rating=-1) as rating1, count (rating=1) as rating2 from mytable
Could you advice on some neat way I could select in the ^ above format?
select SUM(IF(rating=-1,1,0)) AS rating1,
SUM(IF(rating=1,1,0)) AS rating2 from mytable