Trying to query data set multiple times - mysql

What am I doing wrong? Trying to output two COUNTs and need to Group by Grade.
SELECT grade, COUNT(DISTINCT ID) AS total, SUM(case when course like 'AS%'
then 1 else 0 END) Total_As FROM schedule GROUP BY Grade ORDER BY Grade ASC
Only problem is that Total_As is counting grade for multiple instances. Just need to count once if employee took at least one training with Course 'AS%'. The script is currently counting every single 'AS%' taken by employee(ID). Need the "case" part to only count the employee(ID) once to indicate that employee took at least one 'AS%' training or 0 if they did not attend. SUM should not count the employee more than once.

Related

Consolidate multiple results in a single count

I have a table of sales and a table of sales items. Each sale can have multiple items. The sale also has a sales agency and its the agency ID that is used to search (in this example agency id 8).
So the pseudo query is count how many of a specific product type (in the items table are found for a specific agency between 2 dates.
Table layout
agency --->sale--->item1
|-->item2
My query is
SELECT (SELECT COUNT(DISTINCT(sale_id))
FROM sales_order_items AS si
WHERE si.sale_id = s.id AND si.product_type ='1')as COUNT
FROM sales as s
JOIN agency as a ON s.sales_person = a.agency_id
WHERE s.created >= '2018-01-01 00:00:00' AND s.created <= '2018-01-08 23:59:59' AND a.agency_id = '8'
GROUP BY s.sales_person
If I run this, I get
COUNT
1
and if I remove the GROUP BY I get
COUNT
1
1
1
0
1
0
BUT I want the SUM of the numbers so in this example above ...4 items found!
I am using the DISTINCT because there could be many items on the same sale of the product type i am searching for but I just want to find out how many sales have THAT PRODUCT TYPE rather than a count of items on the order. Where am I going wrong?
Help appreciated
Keith

How to use SUM and COUNT mysql in one query

Not sure if this is possible but I have the following
SELECT SUM(rating) as rating FROM details WHERE client_id = '$id'
The rating column contains individual ratings of users (1,2,3,4,5) stars. I need to sum them all to be able to calculate the average, but I would also like to get how many stars of each delimiter that user got as well.
For instance
The user has 3 records and the ratings are (3,4,3). With a SUM of 10 out of 3 records, I get 3.3 average. But I would like as well
3 stars = 2
4 stars = 1
Is it possible to do this with one query?
If I got your question right, you can use AVG() function instead of SUM().
In the following query, it calculates the average rating of this client_id, also counts number of each star as required in question.
SELECT
AVG(rating) as rating,
COUNT(CASE WHEN rating=1 THEN 1 END) as star_1,
COUNT(CASE WHEN rating=2 THEN 1 END) as star_2,
COUNT(CASE WHEN rating=3 THEN 1 END) as star_3,
COUNT(CASE WHEN rating=4 THEN 1 END) as star_4,
COUNT(CASE WHEN rating=5 THEN 1 END) as star_5
FROM
details
WHERE
client_id = ID_HERE
GROUP BY
client_id
SELECT client_id ,sum(if(rating='1',1,0)) onestar,sum(if(rating='2',1,0)) twostar,sum(if(rating='3',1,0)) threestar,sum(if(rating='4',1,0)) fourstar,sum(if(rating='5',1,0)) fivestar, SUM(rating) sumrate,AVG(rating) as avgrate FROM details WHERE client_id = '$id'
or for all clients
SELECT client_id ,sum(if(rating='1',1,0)) onestar,sum(if(rating='2',1,0)) twostar,sum(if(rating='3',1,0)) threestar,sum(if(rating='4',1,0)) fourstar,sum(if(rating='5',1,0)) fivestar, SUM(rating) sumrate,AVG(rating) as avgrate FROM details WHERE group by client_id
It is a bit unclear what exactly you are after, but this might work:
SELECT rating, count(rating) FROM details WHERE client_id = '$id' GROUP BY rating;
Also, not sure how you are getting $id, but if you are getting it from the user, you should be using prepared statements rather than inserting it directly in your SQL code.
You don't need to sum your rating. I believe what you want is to key off the rating. I'd suggest this.
This will group every rating then tell you how many times this client is rated something that many times. This would also help you account for any instances where they made a decimal vote (i.e. 4.5 stars)
SELECT
concat(rating, ' Stars') as rating,
count(*) as count
FROM details
WHERE client_id = '$id'
Group by concat(rating, ' Stars');
I think this should do it. Also, you said you're summing them only to take the average. Why not use AVG() instead.
SELECT COUNT(rating), AVG(rating) as rating FROM details WHERE client_id = '$id' GROUP BY rating

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 count select query with condition

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.

Counting twice within one query

What am I doing wrong? Trying to output two COUNTs and need to Group by Department.
SELECT Department,
COUNT(DISTINCT ID) AS total,
SUM(CASE WHEN course LIKE 'AS%' THEN 1
ELSE 0
END) AS Total_AS
FROM schedule
GROUP BY Department
ORDER BY Department ASC
Only problem is that Total_As is counting Department for multiple instances. Just need to count once if employee(ID) took at least one training with Course AS%. The script is currently counting every single AS% taken by employee(ID). Need the case part to only count the employee(ID) once to indicate that employee took at least one AS% training or 0 if they did not attend. SUM should not count the employee more than once. Tried to use MAX in place of SUM but the results only gave 1 or 0 per Department. Trying to get output of Department, Total Employees, # of Employees in Department that have at least 1 Course Like 'AS%'.
Output should be:
Accounting, Total of 20 employees in Department, Total_AS 10.
Using current SUM, I get a result for Total_AS of 40 because 10 of the Employees(ID) took 4 courses each. Just need Total_AS to be COUNT of Employees who took at least 1 Course, which in this case would be 10.
Change the SUM to a COUNT(DISTINCT):
SELECT Department,
COUNT(DISTINCT ID) AS total,
COUNT(DISTINCT CASE WHEN course LIKE 'AS%' THEN id END) Total_AS
FROM schedule
GROUP BY Department
ORDER BY Department ASC