MySQL count select query with condition - mysql

I have a mysql table called Game which has two columns, Name and Score. I want to select only the Names whose scores have been atleast 100 and atleast twice. In the below example Ron and Mary will get selected. I am not sure how to write the select statement for this.
Game table

Use GROUP BY with a HAVING clause:
SELECT Name
FROM mytable
GROUP BY Name
HAVING COUNT(CASE WHEN Score >= 100 THEN 1 END) >= 2
HAVING clause checks for Name groups, having at least two records with Score >= 100.

Related

How to count rows in MySQL using an IF statement?

I have a relatively simple question but I'm stuck with writing a proper SQL query to display the results that I need. I have a table which stores results from matches with columns indicating the IDs of the players that took part in the match, the winner and another boolean column which let's say indicates whether I want to include that match in the result or not. So the columns are:
player1_id | player2_id | winner_id | use
So winner_id is the value from one of the first two columns depending on which player won. If I want to count how many times a certain player won a game just using the rows where the use flag is up, I can easily do so with:
SELECT COUNT(*) AS total, winner_id
FROM table
WHERE use = 1
GROUP BY winner_id
ORDER BY total DESC
However, I also want to do the same count but for the players that lost their matches. In other words, I want to group not by the winner_id but by the loser id, which would be the value of either player1_id or player2_id depending on which one of them is different from the winner_id. Any clues on how to do that with a simple query that works?
You can do like this to count the loosers:
SELECT
COUNT(*) AS total,
IF(player1_id = winner_id, player2_id, player1_id) AS looser_id
FROM table
WHERE use = 1
GROUP BY looser_id
ORDER BY total DESC

MySQL Conditional count based on a value in another column

I have table that looks like this:
id rank
a 2
a 1
b 4
b 3
c 7
d 1
d 1
e 9
I need to get all the distinct rank values on one column and count of all the unique id's that have reached equal or higher rank than in the first column.
So the result I need would be something like this:
rank count
1 5
2 4
3 3
4 3
7 2
9 1
I've been able to make a table with all the unique id's with their max rank:
SELECT
MAX(rank) AS 'TopRank',
id
FROM myTable
GROUP BY id
I'm also able to get all the distinct rank values and count how many id's have reached exactly that rank:
SELECT
DISTINCT TopRank AS 'rank',
COUNT(id) AS 'count of id'
FROM
(SELECT
MAX(rank) AS 'TopRank',
id
FROM myTable
GROUP BY id) tableDerp
GROUP BY TopRank
ORDER BY TopRank ASC
But I don't know how to get count of id's where the rank is equal OR HIGHER than the rank in column 1. Trying SUM(CASE WHEN TopRank > TopRank THEN 1 END) naturally gives me nothing. So how can I get the count of id's where the TopRank is higher or equal to each distinct rank value? Or am I looking in the wrong way and should try something like running totals instead? I tried to look for similar questions but I think I'm completely on a wrong trail here since I couldn't find any and this seems a pretty simple problem that I'm just overthinking somehow. Any help much appreciated.
One approach is to use a correlated subquery. Just get the list of ranks and then use a correlated subquery to get the count you are looking for:
SELECT r.rank,
(SELECT COUNT(DISTINCT t2.id)
FROM myTable t2
WHERE t2.rank >= r.rank
) as cnt
FROM (SELECT DISTINCT rank FROM myTable) r;

How can i apply Multiple In clause in a single MySQL query

I have list of ids and corresponding creation dates
for Exmple :
1 2014-05-01
2 2014-07-01
3 2014-08-01
Need suggestion regarding writing a MySQL select statement which gives id details after corresponding creation date.
select id,count(*) from id_details where id IN(1,2,3) where resolved_at >(2014-05-01,2014-07-01,2014-08-01) group by id
The date condition for resolved_at column is not correct. Again, if you have two WHERE clause in your query, that as well not correct. You can's specify > condition in IN clause like you are trying. Your query should look like
select id,count(*)
from id_details
where id IN (1,2,3)
and (resolved_at >= '2014-05-01'
and resolved_at <= '2014-08-01')
group by id
I think you are just trying to use IN operator for resolved_at column like
select id,
count(*)
from id_details
where id IN (1,2,3)
and resolved_at IN ('2014-05-01','2014-07-01','2014-08-01')
group by id

MySQL query for weighted voting - how to calculate with values assigned to different columns

I have a voting application that writes values to a mysql db table. It is a preference/weighted voting system so people choose a first option, second option, and third option. These all go into separate fields in the table. I'm looking for a way to write a query that will assign numerical values to the responses (3 for a first response, 2 for a second, 1 for a first) and then display the value with the summed score. I've been able to do this for total number of votes
select count(name) as votes,name
from (select 1st_option as name from votes
union all
select 2nd_option from votes
union all
select 3rd_option from votes) as tbl
group by name
having count(name) > 0
order by 1 desc;
but haven't quite figured out how to assign values to response in each column and then pull them together. Any help is much appreciated. Thanks!
You could do something like this:
select sum(score) as votes,name
from (select 1st_option as name, 3 as score from votes
union all
select 2nd_option as name, 2 as score from votes
union all
select 3rd_option as name, 1 as score from votes) as tbl
group by name;

Error in subquery

im stuck here and i don't know how to fix it.
I have a db table which has users ID, user grade and date when someone has voted for that user (3 fields).
Im trying to read the user that has the highest average grade for todays date, limited to one.
But the problem is that i want to read only users that have 5 or more votes.
My query looks like this, but im getting an error:
SELECT
idusers,
AVG(votes) AS Grade
FROM rank
WHERE (data = '{$dbDate}')
AND ((SELECT count(ID) + 1 FROM rank) AS tmpcount WHERE tmpcount>4)
GROUP BY idusers
ORDER BY Grade DESC
LIMIT 1
Without the tmpcount>4 clause this query is working ok, but I need to count the Id's.
You have to use HAVING to filter the result set on aggregated values such as COUNT (SUM, MIN, MAX, AVG, …):
SELECT idusers, AVG(votes) AS Grade
FROM rank
WHERE (data = '{$dbDate}')
GROUP BY idusers
HAVING COUNT(*) > 4
ORDER BY Grade DESC
LIMIT 1