select user whose child is less than 2 - mysql

i have table structure like this
id
user_id
parent_id
club
1
1
club1
2
2
1
club1
3
3
1
club1
4
4
2
club1
5
5
2
club1
6
6
3
club1
7
7
3
club1
8
8
4
club1
9
9
4
club1
10
10
5
club1
11
11
5
club1
12
12
6
club1
13
13
6
club1
14
14
7
club1
15
15
7
club1
i want to select user_id whose child is less then 2.
refer to above table user 1,2,3,4,5,6 and 7 two child complete so the query should return 8,9,10,11,12,13,14,15
Refer to above table here user_id 1 have two child 2 and 3 , user_id 2 have child 4 and 5 and so on.
i need to select user_id whose child is less than 2

You can do it as follows :
SELECT user_id
FROM club_tree
WHERE user_id NOT IN (
SELECT parent_id
from club_tree
where club = 'club1'
group by parent_id
HAVING count(1) >= 2
)
and club = 'club1';
We Select users that are not in the list of users with more than two childs.
This is to get users with more than two childs :
SELECT parent_id
from club_tree
group by parent_id
HAVING count(1) >= 2
Here you can test it : http://sqlfiddle.com/#!9/eb2c70/3

Can you try this query using left join :
SELECT c.user_id
from club_tree c
left join
(
select parent_id
from club_tree
where club = 'club1'
group by parent_id
HAVING count(1) >= 2
) as c2 on c.user_id = c2.parent_id
where c.club = 'club1' and c2.parent_id is null;
Try it here : http://sqlfiddle.com/#!9/eb2c70/17

Related

How to select average of two colums with same foreign key

I have a table of matches of a game in a tournement.
Here is the mysql table:
id
player1
player1_score
player1_rating
player2
player2_score
player2_rating
tourney_id
round
1
1
9
1.05
2
3
5.34
5
1
2
3
4
5.21
4
9
3.34
5
1
3
5
9
3.52
6
2
5.24
5
1
4
1
9
6.23
3
0
4.74
5
2
5
2
8
9.43
4
9
1.23
5
2
6
3
9
3.41
5
7
6.23
5
2
7
1
9
5.22
4
2
2.43
5
3
8
2
3
4.21
3
9
5.22
5
3
9
5
1
7.31
6
9
3.43
5
3
How can I get the average player ratings for each individual player in a particular tourney?
Please note, there's two columns with player ids (player1, player2)
Based on your requirements (Even though you do not give us your expected results), I assume your code should be like this:
SELECT (SELECT SUM(player1_rating) / COUNT(DISTINCT player1)
FROM your_table GROUP BY player1) as avg_player1,
(SELECT SUM(player2_rating) / COUNT(DISTINCT player2) FROM your_table
GROUP BY player2) as avg_player2
FROM your_table
I revised the query to:
SELECT player_id
, tourney_id
, avg_player_rating = SUM(player_rating) / SUM(cnt)
FROM (
SELECT player_id = player1
, tourney_id
, player_rating = SUM(player1_rating)
, cnt = COUNT(*)
FROM tournament
GROUP BY tourney_id, player1
UNION ALL
SELECT player_id = player2
, tourney_id
, player_rating = SUM(player2_rating)
, cnt = COUNT(*)
FROM tournament
GROUP BY tourney_id, player2
) AS a
GROUP BY player_id, tourney_id
and this will give the result:
player_id tourney_id avg_player_rating
---------- ---------- --------------------
1 5 4.1666
2 5 6.3266
3 5 4.6450
4 5 2.3333
5 5 5.6866
6 5 4.3350
I hope that this is the result that you are looking for.

MySQL: return first row from join

I have 2 tables "quizzes" & "users", I need to return list of first user took each quiz, with quiz_id:
tables structure
"quizzes" structure:
id name
1 England
2 france
3 Japan
4 USA
5 UAE
6 Sweden
7 Italy
8 Brazil
9 South Korea
10 India
"users" structure:
id user_id quiz_id
1 1 1
2 1 2
3 2 1
4 3 4
5 1 4
6 5 9
7 2 9
8 3 8
9 3 9
10 3 7
I need to run query to return first "user_id" took each "quiz", (order by users.id ASC)
expected results:
quiz_id user_id
1 1
2 1
4 3
7 3
8 3
9 5
thanks,
You first group by quiz and pick minimal id and then select based on those ids:
select quiz_id, user_id
from users
where id in(select min(id) from users group by quiz_id)
Select quiz_id, user_id
from users
Where (quiz_id, id) in
(
Select quiz_id, min(id) id
From users
Group by quiz_id
)

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: fill table column from other column with pivot table

I have 3 tables, "questions", "q_t" -> pivot table and "tags":
I need to fill tags.lid with first occurrence in q_t.tid to be = questions.lid
"questions" table structure:
qid lid
1 901
2 901
3 500
4 500
5 200
6 210
7 333
8 423
"q_t" table structure:
qid tid
1 8
2 4
3 8
4 1
1 2
6 3
2 8
8 1
"tags" table structure:
tid lid
1 null
2 null
3 null
4 null
5 null
6 null
7 null
8 null
I need "tags" table to be:
tid lid
1 500
2 901
3 210
4 901
5 null
6 null
7 null
8 901
thanks,
I would go with a query like this:
UPDATE tags t
SET t.lid = (
SELECT q.lid
FROM questions q
JOIN q_t ON (q.qid = q_t.qid)
WHERE q_t.tid = t.tid
LIMIT 1
)
It might not be the best performance-wise, but for a one-time job, it should do.
As an alternative:
UPDATE tags t
JOIN (
SELECT q.lid, q_t.tid
FROM questions q
JOIN q_t USING (qid)
GROUP BY q_t.tid
) d USING(tid)
SET t.lid = d.lid

update statement with aggregate function

Good day everyone..!:-)
I have this table tab where totalUsed is equal to the sum of all used values referenced to name
cid name used total
1 a 1 1
2 a 3 4
3 a 6 10
4 b 3 3
5 b 7 10
6 b 10 0
7 a 5 0
i have this code but it only copy totalUsed's adjacent used value
UPDATE tab
SET totalUsed=
(
SELECT SUM(used)
)
cid name used total
1 a 1 1
2 a 3 3
3 a 6 6
4 b 3 3
5 b 7 7
6 b 10 10
7 a 5 5
if used is set as 10 for cid 6, totalUsed should be 20
and for cid 7 it should be 15.
how to do it in mysql?
it should look like this.
cid name used total
1 a 1 1
2 a 3 4
3 a 6 10
4 b 3 3
5 b 7 10
6 b 10 20
7 a 5 15
thanks for help
:-)
In most dialects of SQL, you can do this using an update with a correlated subquery:
UPDATE tab
SET totalUsed = (SELECT SUM(used)
from tab tab2
where tab2.name = tab.name and
tab2.cid <= tab.cid
);
EDIT:
The above will not work in MySQL. You can do this instead:
UPDATE tab join
(select tab2.cid,
(SELECT SUM(used)
from tab tab3
where tab3.name = tab2.name and
tab3.cid <= tab2.cid
) as cum_used
from tab tab2
) tab2
on tab.cid = tab2.cid
SET tab.totalUsed = tab2.cum_used;