i got stuck for this query..
i just want to sum result of every id, but every id have many rows..
ID SUB Marks
1 English 25
2 English 25
3 English 25
4 Maths 10
5 Maths 10
6 Maths 10
i need the result like this
ID Marks
1 35
i just take one id every group, then i sum.. how do id finished it?
i try SUM( distinct..) but wrong result..
really need your helps guys..
select sum(marks)
from
(
select distinct sub, marks
from your_table
) tmp
Related
Need MySQL query for below problem
Consider a table having student and their marks in a particular subject
Schema
std_id int(11)
marks int(11)
Sample data
std_id marks
1 10
2 15
3 90
4 120
5 25
6 29
7 121
8 122
Now I have an web app in which a form will take a input (int) from user.
For eg 12
then I am required to show total number of student ids (std_id) and their corresponding marks group.
Eg
std_total (tot no of students) group (marks range we got from form)
1 0-11
1 12-23
2 24-35
1 84-95
3 120-131
#Barmar Your answer was almost correct, I made few changes to clean the output. Your query gives output as below :
0-11 2
1-12 2
2-13 1
3-14 1
4-15 1
6-17 1
7-18 2
My query return Outout as
0-11 2
12-23 2
24-35 1
36-47 1
48-59 1
72-83 1
84-95 2
SELECT CONCAT(FLOOR(marks/12)*12, '-', FLOOR(marks/12)+11*(FLOOR(marks/12))+11) AS `group`, COUNT(*) as `std_total`
FROM yourTable
GROUP BY `group`
Use division and FLOOR() to get the beginning of each range.
SELECT CONCAT(FLOOR(marks/12), '-', FLOOR(marks/12)+11) AS `group`, COUNT(*) as `std_total`
FROM yourTable
GROUP BY `group`
i have a table like this
id genre genre2
1 25 0
2 12 25
3 25 12
4 18 17
5 19 14
and i want to count the different genres id and the count of them in both columns but merged into one
and want a result like this
genre count
25 3
12 2
18 1
19 1
17 1
14 1
0 1
i have tried unions, and counts, and everything i think of, but i cant achieve to get the result i want, the closest was making two querys and UNION them, but it doesnt work like i want
any ideas? thanks for the help
You must use UNION ALL instead UNION.
SELECT
genre, COUNT(*) cnt
FROM
(SELECT genre FROM tbl
UNION ALL
SELECT genre2 FROM tbl) a
GROUP BY genre
ORDER BY cnt DESC
You can try it on SQL FIDDLE
I am aggregating data and I cannot sum certain columns so I would like to take the most frequent observation from that column, or the mode value. Each ID can have only one site and number, so if there are ties then pick the smaller of the two numbers.
Example follows:
ID site number
1 3 45
1 3 45
1 2 56
1 3 56
2 4 5
2 5 5
2 5 3
2 5 5
I want it to look like:
ID site number
1 3 45
2 5 5
Here's one way of doing it:
with aggregation as
(
select id
, site
, number
, numberCount = count(1)
from SiteNumbers
group by id
, site
, number
), aggregateRanks as
(
select *
, idRank = row_number() over (partition by id order by numberCount desc, number, site)
from aggregation
)
select id
, site
, number
from aggregateRanks
where idRank = 1
SQL Fiddle with demo.
It matches your results, but depending on all your different cases might need some tweaking; hopefully it gives you some ideas.
I have one voter table which contain large amount of data. Like
Voter_id name age
1 san 24
2 dnyani 20
3 pavan 23
4 ddanial 19
5 sam 20
6 pickso 38
I need to show all voter_name by Alphabetically and count them.Like
name
san
sam
s...
s...
dnyani
ddanial
d...
pavan
pickso
p..
p..
I try using count(voter_name) or GROUP BY.
But both not working for me..... Suppose table contain 50 voters details.
number of person name start with A=15,b=2, c=10,y=3 and so on.
Then how to count and show first 15 record of 'A' person, next 2 record of 'B' person and so on..
Give me any reference or hint..
Thanks in Advance.
It is as simple as this,
SELECT SUBSTRING(name,1,1) as ALPHABET, COUNT(name) as COUNT
FROM voter GROUP BY SUBSTRING(name,1,1);
This order names only:
SELECT `name` FROM `voter` ORDER BY `name` ASC
This counts each occurrence of the first letter and group them group them together
ex.:
Letter COUNT
------ -------
A 15
B 2
C 10
y 3
SELECT SUBSTR(`name`,1,1) GRP, COUNT(`name`) FROM `voter` WHERE
SUBSTR(`name`,1,1)=SUBSTR(`name`,1,1) GROUP BY GRP ORDER BY GRP ASC
Here you go!
If you need names and their counts in ascending order, then you can use:
SELECT
name, COUNT(*) AS name_count
FROM
voter
GROUP BY
name
ORDER BY
name ASC
Which will give the output like
name name_count
------------------
albert 15
baby 6
...
If you need to display all records along with their counts, then you may use this:
SELECT
voter_id, name, age, name_count
FROM
(
SELECT
name, COUNT(name) AS name_count
FROM
voter
GROUP BY
name
) counts
JOIN actor
USING (name)
ORDER BY
name
and you get the output as:
voter_id name age name_count
------------------------------------
6 abraham 26 2
24 abraham 36 2
2 albert 19 1
4 babu 24 4
15 babu 53 4
99 babu 28 4
76 babu 43 4
...
Check the SUBSTRING function of MySQL here
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_substring
And we can use a sub-query to achieve our result.
So using that, how about this
SELECT voter_id, name, age, COUNT(*) AS alphabet
FROM
(SELECT voter_id, name, age, SUBSTRING(name, 1, 1) AS first_letter FROM voter)
AS voter
GROUP BY first_letter
ORDER BY first_letter ASC
I have a table:
quiz userid attempt grade
1 3 1 33
1 3 2 67
1 3 3 90
1 3 4 20
Now, I want the last two attempts i.e., 4 and 3 and I want average grade of these 2 grades i.e, 90 and 20
Could anyone help me?
Use ORDER and LIMIT to get the 2 last attempts and the AVG aggregation function :
SELECT AVG(grade) AS average FROM (
SELECT grade FROM table
WHERE userid = 3
ORDER BY attempt DESC LIMIT 2) AS t
If you want to list both test results separately, with the average in each row, then something like this maybe (otherwise you just need the subquery for the average of the two tests):
SELECT userid, attempt, grade,
( SELECT AVG(grade)
FROM table
ORDER BY attempt DESC LIMIT 0, 2 ) AS avg_grade
FROM table
ORDER BY attempt DESC LIMIT 0, 2;