MySQL: select when count = sum & group by - mysql

I need to select user_id & quiz_id, for users which their count of questions in their quiz = sum of correct, this mean they answer 100% correct
answers table:
quiz_id question_id user_id answer_id correct
1 1 1 1 1
1 2 1 6 0
1 3 1 9 1
2 1 2 1 1
2 2 2 5 1
3 4 1 17 1
3 5 1 21 1
3 6 1 25 1
4 1 3 1 1
5 4 4 18 0
6 1 5 1 1
6 2 5 5 1
7 1 3 2 0
7 2 3 7 0
ex 1:
user 1 took "quiz_id" = 1
count of questions in "quiz_id = 1" = 3
sum of correct = 2
so it's not 100%
user_id = 1 in quiz_id = 1 => will not selected
but user_id = 1 will be selected with quiz_id = 3 cause he got 100%
expected results:
quiz_id user_id
2 2
3 1
4 3
6 5
notes:
quiz could be taken with different users with different number of
questions
quiz_id, user_id unique together (user can not take same quiz twice)
thanks,

You should use an aggregate query with HAVING clause:
SELECT quiz_id, user_id
FROM quiz_answer -- or whatever the name is
GROUP BY quiz_id, user_id
HAVING COUNT(question_id) = SUM(correct)
here you must use HAVING instead of WHERE because
The HAVING clause can refer to aggregate functions, which the WHERE
clause cannot
as specified in the docs.

Related

Merging Multiple Queries - Same Table

I have 1 table called itemmovement : It has Item Id , Quantity In , Quantity Out , Invoice Id, Date. I need to make in one query to show how many pieces are sold and beside the sold column there will be the current on hand quantity .
itemmovement
Id itemid qtyin qtyout invid purchasereturnid date
1 1 10 2019-01-04
2 2 8 2019-01-06
3 2 2 1 2019-01-08
4 1 3 2 2019-01-12
5 2 1 2019-02-04
6 3 4 2019-03-04
7 1 1 3 2019-04-04
8 1 1 1 2019-04-14
9 3 1 2 2019-04-24
I need the query to show this result
Id itemid Sold Quantity OnHandQty
1 1 4 5
2 2 2 7
3 3 0 3
I'm Trying to use this query but not working
SELECT *
FROM
(SELECT itmv.itemid,
sum(itmv.qtyout)-sum(itmv.qtyin)
FROM itemmovement itmv
WHERE (itmv.systemdate BETWEEN '2019-01-01' AND '2019-06-01')
AND invid>0
GROUP BY itmv.itemid) AS result1,
(SELECT sum(itmv2.qtyin)-sum(itmv2.qtyout)
FROM itemmovement itmv2
WHERE itmv.itemid=itmv2.itemid
GROUP BY itmv2.itemid) AS result2
ORDER BY sum(itmv.qtyin)-sum(itmv.qtyout)
I'm getting :
Unknown column 'itmv.itemid' in 'where clause it for this syntax :
where itmv.itemid = itmv2.itemid
Here's your query.
select itemid
, sum(case when COALESCE(invid,0) > 0 then qtyout else 0 end) as Sold_Qantity
, sum(qtyin)-sum(qtyout) as OnHandQty
from itemmovement
group by itemid

MySQL: Group By Max Value Returns Duplicate Results

I have below customer table with max qty.
I need a unique user who has max qty order by its group id.
Input:
id, customer_id, customer_group_id qty
1 1 3 1
2 1 3 10
3 1 3 5
4 2 2 10
5 2 2 1
6 2 2 2
7 3 1 5
8 3 1 10
9 4 4 1
10 4 4 2
11 4 4 2
Output should be:
id, customer_id, customer_group_id, qty
11 4 4 2
10 4 4 2 - This should be not selected
2 1 3 10
4 2 2 10
8 3 1 10
Query:
SELECT * FROM customer
WHERE qty IN ( SELECT MAX(qty) FROM customer GROUP BY customer_id)
ORDER BY customer_group_id DESC;
I tried above query but seems not working.

SQL - selecting count of different value

Assume I have table count and the column is (nilai, id_courses, id_lecturer)
nilai id_courses id_lecturer
----- ---------- -----------
2 1 1
2 1 1
2 1 1
3 1 1
3 1 1
1 2 1
1 2 1
5 2 1
5 2 1
then I want to create view like this :
nilai id_courses id_lecturer count
----- ---------- ----------- -----
2 1 1 3
3 1 1 2
1 2 1 2
5 2 1 2
how to do that in SQL syntax?
I just know how to count 1 value with this code
SELECT COUNT( nilai ) , id_courses, id_lecturer FROM count where nilai=1
I've read this post but its to complex, so I don't know how it's work
You need to count all distinct entries by grouping them. The query
SELECT nilai, id_courses, id_lecturer, COUNT(*) AS count
FROM count GROUP BY nilai, id_courses, id_lecturer
should exactly return the table you posted.

How to calulate SUM previous and current row value in MySQL table?

My table is "Activity_table", which have 4 columns. How can I create a function, which SUM each Peroson 2 previous and current activities?
My Activity_table
ID PID AID Act
1 1 1 12
2 1 2 32
3 2 1 5
4 1 3 21
5 2 2 12
6 2 3 19
7 1 4 11
8 2 4 6
PID-PersonID; AID-ActivitieID; Act-Activitie Value
My target:
ID PID AID Act SUM
1 1 1 12 12
2 1 2 32 44
3 2 1 5 5
4 1 3 21 65
5 2 2 12 17
6 2 3 19 36
7 1 4 11 64
8 2 4 6 37
Sum1=12; Sum2=32+12; Sum3=5; Sum4=21+32+12; Sum5=12+5; Sum6=19+12+5; Sum7=11+21+32; Sum8=6+19+12; Thank you,
To use the two previous and the current row,
SELECT ID,PID,AID,Act,
(SELECT SUM(Act)
FROM Activity_table
WHERE ID <= a.ID
AND ID >= a.ID - 2
AND PID = a.PID)
FROM Activity_table a;
You are taking the SUM according to the PID right? Then the last two rows of your target should be modified as,
7 1 4 11 **76**
8 2 4 6 **42**
The most flexible query is for your requirement is,
SELECT ID,PID,AID,Act,
(SELECT SUM(Act)
FROM Activity_table
WHERE ID <= a.ID
AND PID = a.PID)
FROM Activity_table a;
Then if you need only the flow of SUM of a particular PID, you can change it like this,
SELECT ID,PID,AID,Act,
(SELECT SUM(Act)
FROM Activity_table
WHERE ID <= a.ID
AND PID = a.PID)
FROM Activity_table a
WHERE PID = 2;
Result:
ID PID AID Act SUM
3 2 1 5 5
5 2 2 12 17
6 2 3 19 36
8 2 4 6 42
Sorry for the previous wrong Answers (deleted).
This produces the correct answer based on your example:
SET #LastPID = 0, #Act1 = 0,#Act2 = 0,#Act3 = 0;
SELECT ID,PID,AID,Act,`SUM` FROM
(
SELECT ID,PID,AID,Act,#Act3 := IF(#LastPID != PID, 0,#Act2),#Act2 := IF(#LastPID != PID, 0,#Act1), #Act1 := Act, #Act1 + #Act2 + #Act3 `SUM`, #LastPID := PID
FROM Activity_table
ORDER BY PID,ID
) sm
ORDER BY ID
;

MySQL: update table records by sum from another table

I need to sum all time from "user_answers.time" and update "question.total_time", for all records
note: (user_answers.qid foreign key refer to question.id)
tables structure:
"question" table:
id total_time
1 0
2 0
3 0
4 0
5 0
"user_answers" table:
id qid time
1 1 3
2 1 44
3 2 3.2
4 3 2
5 1 5
6 4 1
I need to finish with "question" table like this:
id total_time
1 52
2 3.2
3 2
4 1
5 0
thanks,
Try this:
UPDATE question
SET total_time = (SELECT SUM(time) FROM user_answers WHERE question.id = user_answers.qid)