I have the following table in MySQL database:
FirstName LastName ClubName
ALAN STURGES Club1
ALESIA BLACKBURN Club2
ALESIA BLACKBURN, 'Club 3'
I want to find out the number of clubs that each student is in.then I want to consolidate the report and show just the count of students based on the number of clubs they are in.
example:
students Number of Clubs
20 1
40 2
The GROUP BY and COUNT() clauses will be your heroes.
/*Knowing the number of clubs per student*/
SELECT COUNT(id) FROM your_tbl GROUP BY col_student_id;
and
/*Knowing the number of students per club*/
SELECT COUNT(id) FROM your_tbl GROUP BY col_club_id;
Modify the above queries to suits your need results. For more information about it, read it here https://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html
i guess
SELECT CONCAT(FirstName, ' ', LastName) as FULLNAME, COUNT(*)
FROM TABLENAME
GROUP BY FULLNAME;
Related
I'm taking a SQL class and I need help with a question.
I have the following table on a phpmyadmin server
patient (id, age)
with a total of 100 patients
I'm told to find the sum of people that have same age using SQL.
I wrote this query:
SELECT COUNT(age)
FROM patient
HAVING (COUNT(age) > 1);
but it returns 100 as a result and when I did this query and found the number of patients who have the same age for each age and calculated the count manually I found 78 and I checked it manually and it's indeed 78.
SELECT COUNT(*)
FROM patient
GROUP BY age
HAVING (COUNT(*) > 1);
What's wrong with my code?
Start with a subquery that gets the counts of people with each age. Then filter this to counts more than 1 so it's just people with the same age.
Then in the main query you use SUM() to add all these counts together, to get the total number of patients.
SELECT SUM(count)
FROM (
SELECT COUNT(*) AS count
FROM patient
GROUP BY age
HAVING count > 1
) AS x
select age, count(*) from patient group by age;
I have an Access table where I can count the occurrences of a column however in addition to count I need the percentage of these occurrences
My table has two columns. Month, Subjects. I can retrieve the count such a Subject A has 10 occurrences, Subject B has 15 and so on in my table.
Have tried these two codes but gives a syntax error
Code 1
SELECT Subjects,
COUNT(Subjects.Subject) AS CountOfSubject,
(SELECT COUNT(Subjects.Subject))/(SELECT SUM(Subjects.CountofSubject))) AS PercentofSubject
FROM Subjects
GROUP BY Subject;
Code 2
SELECT Subjects,
COUNT(Subjects.Subject) AS CountOfSubject,
(SELECT AVG(Subjects.CountofSubject)) AS PercentofSubject
FROM Subjects
GROUP BY Subject;
However I need additional column where I can show percentage value of these subject occurrences. Thanks a lot
The big problem I see with your code is that you are not normalizing the counts properly by using a complete subquery. Try this version:
SELECT
Subject,
COUNT(*) AS CountOfSubject,
100.0 * COUNT(*) / (SELECT COUNT(*) FROM Subjects) AS PercentOfSubject
FROM Subjects
GROUP BY Subject;
i.e. a table called students (Assume there is only one student with that name, e.g. there isnt two students called John Smith) with
studentName, studentScore, subject
assume table has this
John Smith 40 maths
bob grey 20 english
anne hank 23 english
John Smith 30 english
anne grey 10 maths
I am trying to find the most highest scored student by calculating the maximum of averages of all students
this here will select the highest average but not the student name with that average:
SELECT MAX(avgStudentScore)
FROM (SELECT AVG(studentScore) AS avgStudentScore FROM students GROUP BY studentName) t
thx
If all you need is the name, you can do something like this:
select studentName, avg(studentScore) as avgStudentScore
from students
group by studentName
order by avgStudentScore desc
limit 1
This will return only the first row of the query. Since it's ordered by avgStudentScore, it will return the student with the top average.
The above solution is the simplest and fastests, but it's not the only one. If you want to do it "the hard way" (with subqueries), what you need to do is:
Calculate the average for each student
Get the highest average
Filter the student with the highest average
So... let's do it the hard way ;)
select a.studentName
from
(
select studentName, avg(studentScore) as avgStudentScore
from students
group by studentName
) as a
where
a.avgStudentScore = (
select max(avgStudentScore)
from (
select avg(studentScore) as avgStudentScore
from students
group by studentName
) as a
)
Notice that with this approach the final result might not be unique (i.e. there may be one ore more students with the same average and that average is the highest).
Use this query:
SELECT studentName, max( mark ) as maxMark FROM `student` GROUP BY studentName
I have a table for example:
Names Details
--------------
wilson admin
david member
wilson admin
wilson admin
sam member
david member
Now what i want to achieve is show a table like this
Names Details count
--------------------
wilson admin 3
david member 2
sam member 1
I want to show the names according to how many times they appear
SELECT Name, Detail, COUNT(*) as Count
FROM MyTable
GROUP BY Name, Detail
ORDER BY Count DESC;
Since you have more than one column in your output table SELECT, you should group by both columns.
I found the answer already
SELECT names, COUNT(names) AS count
FROM my_table
GROUP BY names ORDER BY count DESC
Try this, use the GROUP BY and COUNT
SELECT Names, Details, COUNT(NAMES) as `count` FROM `table` GROUP BY `Names`
Im having problems trying to understand how to build this query.
My data scheme:
id category name userid
1 sports football 1
2 cars ferrari 1
3 sports basketball 1
4 film Matrix 9
5 film Fauno 9
6 sports Surf 3
As you can see the category can be repeated even for a same user id because the name field is different. So my idea is to get the categories of a set of users and the amount of users for each categories on that set.
Lets say i have the set of user set_of_user = (1,9,3), If i run the query
SELECT something FROM category_table WHERE userid IN set_of_user SOME CONDITION HERE
The correct result should be:
category: sports
users: 2
category: cars
users: 1
category: film
users: 1
My best shot was:
SELECT userid, category, COUNT(userid) as users from interests WHERE `userid` in ' . $norm_info_ids . ' GROUP BY category
But this gave a bad result, how can i solve this?
I think this is what you're looking for using COUNT with DISTINCT, along with GROUP BY:
select category, count(distinct userid)
from interests
where userid in (1,3,9)
group by category
SQL Fiddle Demo
Resulting in:
CATEGORY COUNT(DISTINCT USERID)
cars 1
film 1
sports 2
First you need to select the distinct paring of category and user id
select t.category, count(t.user_id)
from
(
SELECT distinct category,user_id
from tab
where user_id in (1,3,9)
) t
group by t.category