I have a table like this:
name | day | score
------------------
John | 1 | 4
John | 2 | 5
John | 3 | 6
Marc | 1 | 7
Marc | 2 | 4
Marc | 3 | 5
Paul | 1 | 8
Paul | 2 | 2
Paul | 3 | 3
I want to get the sum of the score for each person, but only for certain days, sorted by this sum. let's say I want to get the score-sum of the 1. and 2. day, this is what I expect:
name | sum(score)
-----------------
Marc | 11
Paul | 10
John | 9
this is what failed:
SELECT name, sum(score) FROM mytable WHERE day<=2
I think I have to surround the sum(score)-part with some IF-statement, but I have no idea how.
Just add group by
SELECT name, sum(score) FROM mytable WHERE day<=2 group by name
Use sum function and group by clause for grouping the result.
query
select name,sum(score) as score
from myTable
where day in (1,2)
group by name
order by sum(score) desc;
fiddle demo
Related
results table:
result_event | name | position
-------------+---------------+------------
1 | Jason Smith | 1
1 | David Johnson | 2
1 | Randy White | 3
1 | Billy Hansen | 4
2 | Wally Mann | 1
2 | Jason Smith | 2
2 | Billy Hansen | 3
2 | David Johnson | 4
2 | Randy White | 5
I have a table with race results as above. I want to sort the riders by their combined placement in the two races. (eg 1st plus 2nd = 3, 2nd plus 4th = 6, etc.) Racer "Wally Mann" did not race the first race so even though he won the second race, he should be scored behind all others.
Desired Result:
Name | Race1 | Race2
--------------+--------+-------
Jason Smith | 1 | 2
David Johnson | 2 | 4
Billy Hansen | 4 | 3
Randy White | 3 | 5
Wally Mann | NULL | 1
Current query:
SELECT name,
CASE(WHEN result_event=1 then position else 0 END) Race1,
CASE(WHEN result_event=2 then position else 0 END) Race2,
SUM(position) eventscore
FROM results
GROUP BY name
ORDER BY eventscore DESC
In my current query, "Wally Mann" is first in the list because (Null + 1) < (1+2). What can I do to make the (Null + 1) result sort AFTER all the racers who have two results?
Change your ORDER BY to sort by number of records DESC and THEN BY eventscore
ORDER BY count(*) DESC, eventscore ASC
select name from results
group by name
order count(*) desc, sum(position);
I apologize in advanced if I am not explaining this correctly. I can barely explain it in english terms, let alone in a mysql query.
I am trying to get the list of response_set_ids that have more than 1 record for a question_id.
This is an example of my data:
+----+-----------------+-------------+-----------+
| id | response_set_id | question_id | answer_id |
+----+-----------------+-------------+-----------+
| 1 | 10 | 1 | 4 |
| 2 | 10 | 2 | 5 |
| 3 | 10 | 3 | 6 |
| 4 | 10 | 3 | 7 |
| 5 | 11 | 1 | 8 |
| 6 | 11 | 2 | 9 |
| 7 | 11 | 3 | 10 |
+----+-----------------+-------------+-----------+
I would like to have a query that would return me a list response_set_ids, and in this particular example, I would expect to get returned 10 because that response_set has question_id -> 3 showing up more than once.
Please let me know if you need any further information to help me.
I have tried this:
select response_set_id, count(question_id) from responses group by response_set_id;
But that only gives me the counts of questions per response_set.
Thank you in advanced!
The simplest method doesn't use a subquery:
SELECT DISTINCT response_set_id
FROM responses
GROUP BY response_set_id, question_id
HAVING COUNT(*) > 1;
This is one of the very, very few instances where select distinct is used (appropriately) with group by.
select distinct response_set_id from (
select response_set_id , question_id
from
responses
group by
response_set_id, question_id
having count(*)>1) a
I believe this question has been asked before but I cannot add comments at my current rep:
SELECT DISTINCT response_set_id
FROM responses
GROUP BY question_id
HAVING COUNT(question_id) > 1
I'm sorry for fuzzy title of this question.
I have 2 Tables in my database and want to count records of first_table using "group by" on a foreign key id that exists in a column of second_table (which stores ids like array "1,2,3,4,5").
id | name | fk_id
1 | john | 1
2 | mike | 1
3 | jane | 2
4 | tailor | 1
5 | jane | 3
6 | tailor | 5
7 | jane | 4
8 | tailor | 5
9 | jane | 5
10 | tailor | 5
id | name | fk_ids | s_fk_id
1 | xxx | 1,5,6 | 1
2 | yyy | 2,3 | 1
3 | zzz | 9 | 1
4 | www | 7,8 | 1
Now i wrote the following query but it not working properly and displays wrong numbers.
I WANT TO:
1-Count records in first_table group by "fk_id"
2-Sum the counted records which exists in "fk_ids"
3-Display the sum result (sum of related counts) grouped by id.
symbol ' ' means ``.
select sum(if(FIND_IN_SET('fk_id', 'fk_ids')>0,'count',0) 'sum', 'count', 'from'.'fk_id', 'second_table'.* FROM 'second_table'
LEFT JOIN
(
SELECT 'fk_id', count(*) 'count'
FROM 'first_table'
group BY 'fk_id'
) AS 'from'
ON FIND_IN_SET('fk_id', 'fk_ids')>0
WHERE 'second_table'.'s_fk_id'=1
GROUP BY 'id'
ORDER by 'count' DESC
This table has many data and we have no plan to change the structure.
Edit:
Desired output:
id | name | sum
1 | xxx | 7 (3+4+0)
2 | yyy | 2 (1+1)
3 | zzz | 0 (0)
4 | www | 0 (0+0)
After two holidays i came back to work and found out that the "FIND_IN_SET" function is not working properly with space contained string.
And the problem is that i was ignored the spaces too, (same as this question)
Finnaly this query worked:
select sum(`count`) `sum`, `count`, `from`.`fk_id`, `second_table`.* FROM `second_table`
LEFT JOIN
(
SELECT `fk_id`, count(*) `count`
FROM `first_table`
group BY `fk_id`
) AS `from`
ON FIND_IN_SET(`fk_id`, replace(`fk_ids`,' ',''))>0
WHERE `second_table`.`s_fk_id`=1
GROUP BY `id`
ORDER by `count` DESC
And the magic is replace(fk_ids,' ','')
Basically, as the question states, I am looking for the easiest and most straight forward way of getting counts based on unique values.
Here is my data set:
id | item_id | user_id
1 | 10 | 123
2 | 10 | 123
3 | 10 | 123
4 | 11 | 123
5 | 12 | 123
6 | 10 | 456
7 | 10 | 789
8 | 12 | 456
Ideally, when I run the query I should get the following:
count | user_id
3 | 123
2 | 456
1 | 789
Where even though user 123 has 5 items to their name, the really only purchased 3 unique items. Is this really straight forward and I'm just missing it completely? Here is what I have currently:
SELECT count(user_id) AS count, item_id, user_id
FROM table
GROUP BY item_id, user_id
HAVING count > 1
ORDER BY count DESC
This is producing the opposite of what I want:
count | user_id
5 | 123
2 | 456
1 | 789
Thanks in advance! And if this has been answered already, please point me in that direction.
You can count distinct item ids and then group by the user id:
SELECT
COUNT(DISTINCT item_id) AS count,
user_id
FROM
event_assigned
GROUP BY
user_id
ORDER BY
count DESC
Guys i want to get the top 3 disease and also their count from following table for a particular year..what query should i run?
mysql> select id,dname,d_id,entrydate from patient_master;
+----+------------------+------+------------+
| id | dname | d_id | entrydate |
+----+------------------+------+------------+
| 1 | Root Canal | 1 | 2012-08-02 |
| 2 | Cosmetic Filling | 3 | 2012-05-10 |
| 3 | Root Canal | 1 | 2012-05-25 |
| 4 | High BP | 6 | 2012-07-09 |
| 5 | Root Canal | 1 | 2012-07-10 |
| 6 | Normal Filling | 2 | 2012-05-10 |
| 7 | Maleria | 4 | 2012-07-10 |
| 8 | Maleria | 4 | 2012-07-12 |
| 9 | Typhoid | 5 | 2012-07-12 |
+----+------------------+------+------------+
9 rows in set (0.00 sec)
Use a group by clause to combine results by disease, and count(*) to count the number of records for each disease. You can then order from largest to fewest and use limit 3 to get only the top 3. I have also included a where clause to filter for only records in 2012.
select count(*), dname
from patient_master
where entrydate between '2012-01-01' and '2013-01-01'
group by dname
order by count(*) desc
limit 3
Demo: http://www.sqlfiddle.com/#!2/89c06/6
SELECT d_id, dname, count(d_id) as `count`, year(entrydate) as `year`
FROM patient_master
GROUP by `year`, d_id
ORDER BY `year` DESC, `count` DESC
Note I didn't put a limit here, as if you want to get both year and count in the same query, you would need to get into writing a pretty complex query to get the top 3 per year.
This will sort by year descending and then by disease count descending within each year. You will note be able to get the row id in this query, nor should you care about that value given what you are trying to do.