I make a query, that show each user answer amount of question for each department and correct and incorrect percent of each user when they answer question for that department.
So, I try a query like this,
SELECT d.department_id, d.department_name as department,
a.username, COUNT(a.username)total,
( (COUNT(r.is_correct)*100) /
( SELECT COUNT(a.username) total
FROM qa_report r
LEFT JOIN tbl_user a ON (r.user_id = a.admin_id)
LEFT JOIN department d on (a.department_id = d.department_id)
WHERE d.department_name = 'Dept1' AND is_correct='yes'
)
) as correct_percent
FROM qa_report r
LEFT JOIN tbl_user a ON (r.user_id = a.admin_id)
LEFT JOIN department d on (a.department_id = d.department_id)
WHERE d.department_name = 'Dept1'
GROUP BY a.username
This is the result,
total column is the amount of question's answer that user answer for that department.
In this total, correct and incorrect answer are mixing.
but the correct_percent column is still wrong when I try to calculate correct/incorrect percent of each department.
The problem is at this line,
( (COUNT(r.is_correct)*100) /
( SELECT COUNT(a.username) total
FROM qa_report r
LEFT JOIN tbl_user a ON (r.user_id = a.admin_id)
LEFT JOIN department d on (a.department_id = d.department_id)
WHERE d.department_name = 'Dept1' AND is_correct='yes'
)
) as correct_percent
I need to get the correct percent of each user at that place like this,
(correct percent of answer*100)/total question that user answer
eg. John => total: 10, correct 5, incorrect 5, correct %=50%
I very appreciate for any suggestion.
I can solve this problem now. I was wrong when I try to calculate percentage.
Here is the query that work,
SELECT d.department_id, d.department_name as department,
a.username, COUNT(a.username)total,
( (COUNT(CASE WHEN r.is_correct='yes' THEN 1 END)*100) / ( COUNT(a.username)) ) as correct_percent,
( (COUNT(CASE WHEN r.is_correct='no' THEN 1 END)*100) / ( COUNT(a.username)) ) as incorrect_percent
FROM qa_report r
LEFT JOIN tbl_user a ON (r.user_id = a.admin_id)
LEFT JOIN department d on (a.department_id = d.department_id)
WHERE d.department_name = 'Dept1'
GROUP BY a.username
Hope this help for someone.
Related
[DB Table]
SELECT b.first_name, b.last_name, a.pod_name, a.category, c.user_id,
SUM(IF(QUARTER(CURDATE())-1 OR (QUARTER(CURDATE())-2) AND a.user_id, 1, 0)) AS flag FROM kudos a
INNER JOIN users b ON a.user_id = b.id INNER JOIN users_groups c ON a.user_id = c.user_id
INNER JOIN groups d ON c.group_id = d.id WHERE a.group_name = 'G2' AND d.id IN (7,8,9,11,12,13,14,15,16,17,21,22,23,24,25,26,27,28)
AND QUARTER(CURDATE())-1 = a.quarter ORDER BY a.final_score+0 DESC
I need to get the user_ids of those users which are both in quarter 1 and 2 from table.
Tried above query but failed to get expected results.
Can someone please guide me on this?
if you only need user_id then you can do this :
select user_id
from tablename
where quarter in (1,2)
group by user_id
having count(distinct quarter) = 2
another way is to use window function, assuming you have one user id in each quarter:
select * from (
select * , count(*) over (partition by user_id) cn
from tablename
where quarter in (1,2)
) t where cn = 2
I tried to answer this question here in the code below, but it keeps giving me an error message!
I've tried to figure out how to
Provide the name of the sales_rep in each region with the largest amount of total_amt_usd sales?
and it gave me this Error :
aggregate function calls cannot be nested
ERD picture here
could you please help me with this?
WITH
account_info AS (Select * from accounts),
orders_info AS (select * from orders),
region_info AS (select * from region),
sales_reps_info AS (select * from sales_reps)
SELECT s.name as rep_name, r.name as region_name, MAX (SUM (o.total_amt_usd)) as total
FROM orders_info o
JOIN account_info a
ON o.account_id = a.id
JOIN sales_reps_info s
ON a.sales_rep_id = s.id
JOIN region_info r
ON r.id = s.region_id
GROUP BY TOTAL, REP_NAME, R.NAME
ORDER BY 3 DESC
When you are using the whole table there is no need for WITH
SELECT s.name as rep_name, r.name as region_name, MAX (SUM (o.total_amt_usd)) as total
FROM orders o
JOIN account a
ON o.account_id = a.id
JOIN sales_reps s
ON a.sales_rep_id = s.id
JOIN region r
ON r.id = s.region_id
GROUP BY TOTAL, REP_NAME, R.NAME
ORDER BY 3 DESC
LIMIT 100;
I'm not sure what you are attempting with with since you don't actually define a Common Table Expression.
That aside, your query is invalid, you cannot nest aggregate functions and you are already getting the max 100 by ordering and limiting rows, so I think you just want
SELECT s.name as rep_name, r.name as region_name, SUM (o.total_amt_usd) as Total
FROM orders_info o
JOIN account_info a ON o.account_id = a.id
JOIN sales_reps_info s ON a.sales_rep_id = s.id
JOIN region_info r ON r.id = s.region_id
GROUP BY REP_NAME, R.NAME
ORDER BY Total DESC
LIMIT 100;
i am trying to sum the result of a select that return several rows:
i have four tables (user, team, pilot, result):
user (id_user)
team (id_user, id_team)
pilot (id_pilot, id_team)
result (id_pilot, id_gp, points)
When i do that:
SELECT p.id_team, SUM( r.points ) AS TOTAL
FROM pilot p
LEFT JOIN result r ON p.id_pilot = r.id_pilot
WHERE r.id_grand_prix = '1'
GROUP BY p.id_team
i get:
id_team TOTAL
-------------
1 10
2 15
3 5
4 6
5 7
what i want is, if id_user has id_team 1 and 2 has 25 points.
i try that:
UPDATE user AS u
JOIN has_team ON u.id_user = has_team.id_user
JOIN (
SELECT p.id_team, SUM( r.position ) AS TOTAL
FROM pilot p
LEFT JOIN result r ON p.id_pilot = r.id_pilot
WHERE r.id_grand_prix = '1'
GROUP BY p.id_team
) AS grp
ON grp.id_team = has_team.id_team
SET u.points = grp.TOTAL + u.points
but i only have the points of one team.
thanx very much in advance how can help me.
Are you looking to get the SUM of points for each user, instead of each team?
If so, your query might be something like this:
SELECT u.id_user, SUM( r.points ) AS TOTAL
FROM pilot p
LEFT JOIN result r ON p.id_pilot = r.id_pilot
INNER JOIN team t ON p.id_team = t.id_team
INNER JOIN user u ON t.id_user = u.id_user
WHERE r.id_grand_prix = '1'
GROUP BY u.id_user
If not, could you clarify what you need to display?
This sample code her just figures out the grade average for table 3, not the true GPA : SELECT *
FROM (
SELECT AVG(g.Grade) as average_grade, g.SSN
FROM Grade g
group by g.SSN) a
inner join Student s on a.ssn = s.ssn;
Tying to figure out the correct formula: Here is what I have came up with but it is not correct
SELECT *
FROM (
SELECT SUM(Grade*CreditHour)/(SUM(CreditHour) as average_grade, g.SSN
FROM Grade g
group by g.SSN) a
inner join Student s on a.ssn = s.ssn;
Need some help stuck on this problem?
It looks to me as if you simply forgot to join the grade table with the course table. Or am I missing something?
SELECT *
FROM
(
SELECT
SUM(g.Grade*c.CreditHour) / SUM(c.CreditHour) as average_grade,
g.SSN
FROM Grade g
INNER JOIN Course c ON c.cno = g.cno
GROUP BY g.SSN
) a
INNER JOIN Student s ON a.ssn = s.ssn;
How can I optimize this query? If you need the table structure just let me know, but this is being done as a custom plugin for Vanilla forums
SELECT
G.*
FROM
(
SELECT
D.DiscussionID,
A. NAME AS Category,
D. NAME,
U. NAME AS USER,
D.CountComments,
IFNULL(
MAX(C.DateInserted),
D.DateInserted
) AS Updated
FROM
GDN_Discussion AS D
LEFT OUTER JOIN GDN_Comment AS C ON D.DiscussionID = C.DiscussionID
INNER JOIN GDN_Category AS A ON A.CategoryID = D.CategoryID
LEFT OUTER JOIN GDN_User AS U ON U.UserID = D.InsertUserID
WHERE
A.CategoryID = '626'
GROUP BY
D.DiscussionID
) AS G
ORDER BY
G.Updated DESC
LIMIT 0, 10