displaying count of user id per count of answer - mysql

I have a table containing answer data answered by a unique user. So I have this query.
select ta.user_id, count(ta.answer_id) count_answer_id
from survey_answers ta
group by ta.user_id
having count(ta.answer_id) < 124
order by count_answer_id desc;
userid count of answers
3702 123
2866 120
5483 120
565 120
1292 120
621 119
2250 119
2719 119
4192 119
5539 119
354 119
1441 119
2501 115
1636 115
866 109
53 108
3091 107
329 106
285 105
997 104
1352 103
5281 103
430 102
2125 102
But I would like to get the results like this.
user count answer count
1 123
4 120
7 119
2 115
.
.
.
.

select count(*) as user_count, count_answer_id
from
(
select ta.user_id, count(ta.answer_id) count_answer_id
from survey_answers ta
group by ta.user_id
having count(ta.answer_id) < 124
) tmp
group by count_answer_id
order by count_answer_id desc

Related

I want to return the number of rows it's been since a value occurred

Alright, I'm starting over with a new bit of code that feels more efficient and shows that I can get some of what I want but not the final bit. Here is the edited code:
SELECT
uq1.UserID,
uq1.QuestionID,
r.LastAttempt,
r.Score,
r.DateSeen,
s.LastBad,
s.Score AS ScoreUnder,
s.DateSeen AS DateScoreUnder
FROM
users_questions uq1
LEFT JOIN (
SELECT
uq2.QuestionID,
uq2.UserID,
MAX(UsersQuestionsID) LastAttempt,
uq2.Score,
uq2.DateSeen
FROM
users_questions uq2
GROUP BY
uq2.UserID,
uq2.QuestionID
) AS r ON r.QuestionID = uq1.QuestionID AND r.UserID = uq1.UserID
LEFT JOIN (
SELECT
uq3.QuestionID,
uq3.UserID,
MAX(UsersQuestionsID) LastBad,
uq3.Score,
uq3.DateSeen
FROM
users_questions uq3
WHERE
uq3.Score <= 250
GROUP BY
uq3.UserID,
uq3.QuestionID
) AS s ON s.QuestionID = uq1.QuestionID AND s.UserID = uq1.UserID
GROUP BY
uq1.UserID,
uq1.QuestionID
It seems to be more efficient and produce some of what I want. The thing I need to find is the number of iterations between the record that it displays on the last LEFT JOIN and the end of the table for each question and user combo.
My test data is this:
UsersQuestionsID
Score
DateSeen
UserID
QuestionID
1
877
2022-06-25 19:51:18
14
98
2
765
2022-06-25 15:52:42
14
99
3
345
2022-06-25 15:54:22
14
99
4
754
2022-06-25 15:54:22
14
107
5
222
2022-06-25 16:11:40
13
73
6
525
2022-06-25 16:11:40
13
55
7
23
2022-06-25 16:11:40
13
130
8
888
2022-06-25 16:11:40
13
104
9
234
2022-07-01 12:41:44
14
94
10
564
2022-07-01 12:41:44
14
106
11
0
2022-07-01 13:59:37
14
99
12
267
2022-07-01 16:57:40
14
99
13
345
2022-07-02 15:17:03
13
99
14
49
2022-07-02 17:44:19
14
99
15
222
2022-07-02 18:10:17
14
99
16
49
2022-07-02 18:10:43
14
94
17
1000
2022-07-02 18:11:18
14
106
18
0
2022-07-06 14:18:51
14
94

how do i join two different tables in mysql

This is table1:
id name m1 m2 m3 total itemno
1 raj 10 10 10 30 1
2 ram 60 60 60 180 1
3 kumar 70 70 70 210 1
4 kanna 50 50 50 150 1
5 vivek 64 64 91 200 1
5 vivek 90 90 90 270 2
This is table2:
id name mark1 mark2 mark3 itemno
101 vivek 78 78 78 1
102 vivekkanna 89 88 78 1
103 rajagopalan 97 90 98 1
104 kumar 69 54 56 1
101 vivek 90 90 90 2
I want to join these two tables like this into a result set that looks like this:
id name m1 m2 m3 total mark1 mark2 mark3 item no
1 raj 10 10 10 30 0 0 0 1
2 ram 60 60 60 180 0 0 0 1
3 kumar 70 70 70 210 69 54 56 1
4 kanna 50 50 50 150 0 0 0 1
5 vivek 64 64 91 200 78 78 78 1
5 vivek 90 90 90 270 90 90 90 2
Seems you want a regular LEFT JOIN, returning a default value if the row in table2 does not exist;
SELECT t1.id, t1.name, t1.m1, t1.m2, t1.m3,
COALESCE(t2.mark1, 0) mark1, COALESCE(t2.mark2, 0) mark2, t1.itemno
FROM table1 t1
LEFT JOIN table2 t2 ON t1.name = t2.name AND t1.itemno = t2.itemno
ORDER BY t1.id, t1.itemno
An SQLfiddle to test with
We use a LEFT JOIN to get the default NULL value for all table2 fields that don't match with a row in table1, then COALESCE to turn them into 0 instead.

select query to get those result where for every unique value of column 3, column 2 has bothval1 and val2

MEMO TYPE ID
2442 11 52
33658 4 52
823 6 56
825 4 56
826 7 56
85 4 57
3298 7 57
87 4 141
377 7 141
88 4 142
378 7 142
98 1 143
99 2 143
194 7 143
7586 5 143
1451 4 143
7781 6 143
3252 4 167
3249 6 167
3915 7 167
13666 5 167
115 4 168
9253 9 168
9254 10 168
138 1 194
139 2 194
1951 4 194
8650 7 194
8191 6 197
8192 7 197
9687 8 197
9930 9 197
I need to select those records from above table where for every unique value in column 'ID', column 'TYPE' have value 6 and 7 both.
Result of this select query would be as below:
MEMO TYPE ID
823 6 56
826 7 56
7781 6 143
194 7 143
3249 6 167
3915 7 167
8191 6 197
8192 7 197
I hope this data is not too much.
select t.*
from your_table t
inner join
(
select id, min(type) as mint, max(type) as maxt
from your_table
where type in (6,7)
group by id
having count(distinct type) = 2
) x on x.id = t.id and t.type in (x.maxt,x.mint)
SQLFiddle demo

Get rows that have common value from the same table with different id

I just reviewed this question but what I need is something quite different.
However, I have this table:
team_players_order
(team_id,
player_id,
ordering,
captain
)
The table stores a list of teams ids along with their players ids that belong to each team (each team could get 0-15 players). What I want to get is the teams that have a common players among them.
The list of teams I want to compare could be known (I have their ids)or unknown, then I might need to search the whole table and compare all teams. .
Here's a sample data for three teams:
team_id player_id ordering captain
117 134 0 N
117 55 1 N
117 97 2 N
117 215 3 N
117 165 4 N
117 221 5 N
117 163 6 N
117 128 7 N >> common player
117 180 8 N
117 96 9 N
117 162 10 N
117 88 11 N
117 229 12 N
117 91 13 N
117 105 14 N
-----------------------------------------------
124 88 0 N
124 165 1 N
124 92 2 N
124 130 3 N
124 47 4 N
124 221 5 N
124 30 6 N
124 223 7 N
124 105 8 Y
124 6 9 N
124 96 10 N
124 120 11 N
124 198 12 N
124 128 13 N >> common player
124 202 14 N
-----------------------------------------------
125 256 0 N
125 58 1 N
125 10 2 N
125 47 3 N
125 103 4 N
125 167 5 N
125 221 6 N
125 128 7 N >> common player
125 105 8 N
125 96 9 Y
125 180 10 N
125 210 11 N
125 229 12 N
125 30 13 N
125 33 14 N
As you can see, player 128 is common player among these three teams. I need to find other common players as well.
What I have tried so far is the following query which I guess is comparing each giving team with all other teams and get any common player that exists individually of each comparison.
SELECT
t1.team_id,
t1.player_id,
t2.team_id,
t2.player_id
FROM team_players_order AS t1
INNER JOIN team_players_order AS t2
ON (t1.team_id != t2.team_id
AND t1.player_id = t2.player_id)
WHERE t1.team_id IN(117,124,125)
AND t2.team_id IN(117,124,125)
ORDER BY t1.team_id, t2.team_id
which returns:
team_id player_id team_id player_id
117 221 124 221
117 88 124 88
117 96 124 96
117 105 124 105
117 128 124 128
117 165 124 165
117 180 125 180
117 221 125 221
117 229 125 229
117 96 125 96
117 105 125 105
117 128 125 128
124 128 117 128
124 165 117 165
124 221 117 221
124 88 117 88
124 96 117 96
124 105 117 105
124 128 125 128
124 30 125 30
124 221 125 221
124 47 125 47
124 96 125 96
124 105 125 105
125 128 117 128
125 180 117 180
125 221 117 221
125 229 117 229
125 96 117 96
125 105 117 105
125 128 124 128
125 221 124 221
125 30 124 30
125 47 124 47
125 96 124 96
125 105 124 105
But what I want is:
the players that exist in all giving teams (by their ids)
the player that exist in all teams.
n.b. the list of teams could reach to 100 once it's given.
HAVING is the solution
the player that exist in all teams
select team_id, player_id, count(*) nb from team_players_order
group by player_id
having nb > 1
the players that exist in all giving teams
select team_id, player_id, count(*) nb from team_players_order
where team_id in (124, 117)
group by player_id
having nb > 1
You can use "HAVING COUNT" in order to know which player is in multiple teams.
SELECT player_id FROM team_players GROUP BY team_id HAVING COUNT(team_id) > 1
And to list teams that have a player in other teams :
SELECT team_id FROM team_players GROUP BY team_id HAVING COUNT(player_id) > 1
Hope that helps !

Sorting varchar fields for mysql database

Trying to sort the following TEAM_TOTAL Column Desc
MATCHID TEAM_TOTAL
---------- -----------------
573 Total 112
573 Total 2 for 115
574 Total 9 for 97
574 Total 2 for 100
575 Total 9 for 129
575 Total 9 for 101
576 Total 4 for 191
576 Total 9 for 160
577 Total 8 for 157
577 Total 7 for 137
578 Total 6 for 193
578 Total 119
But the problem is TEAM_TOTAL is varchar, is there a way with query alone I can get the results in the sorted desc order.
More over there is a text as well in that column. I am running out of ideas to get this up
Result should have beeen like the below Result Set
MATCHID TEAM_TOTAL
---------- -----------------
578 Total 6 for 193
576 Total 4 for 191
576 Total 9 for 160
577 Total 8 for 157
577 Total 7 for 137
575 Total 9 for 129
578 Total 119
573 Total 2 for 115
573 Total 112
575 Total 9 for 101
574 Total 2 for 100
574 Total 9 for 97
Give this a try:
select * from t
order by substring(
team_total, locate('for', team_total) +
if(locate('for', team_total) > 0, 4, 7))+0 desc
Fiddle here.
Try to extract the integer (string after the last space):
-- 'Total 112' - extracts 112
SELECT SUBSTRING('Total 112', LENGTH('Total 112') - LOCATE(' ', REVERSE('Total 112')));
-- 'Total 6 for 193' - extracts 193
SELECT SUBSTRING('Total 6 for 193', LENGTH('Total 6 for 193') - LOCATE(' ', REVERSE('Total 6 for 193')));
Now, you can convert that string to a number and then order by it.
SELECT * FROM teams
ORDER BY
CAST(SUBSTRING(TEAM_TOTAL, LENGTH(TEAM_TOTAL) - LOCATE(' ', REVERSE(TEAM_TOTAL))) AS INT) DESC