Any Advice with Mysql Count - mysql

i have a t_class table in mySql,
in this table there are 3 columns, No, CLASS and POINT.
and there are approximately 5000 records in this table. i want the count of classes in this table.
No CLASS POINT
1 9 100
2 10 70
3 11 80
4 9 90
5 10 50
6 M 60
7 M 70
8 9 40
9 10 90
10 11 90
11 M 80
12 M 75
13 11 40
14 10 100
15 9 60
As you see there 4 types of classes - 9, 10, 11 and M.
But there is one problem. When it calculates the count of classes it must
summarize 11-th and M th classes. For example
CLASS COUNT
9 4
10 4
11 7
Thanks.

SELECT CLASS, COUNT(*) AS CNT
FROM table
GROUP BY CASE WHEN CLASS='M' THEN '11' ELSE CLASS END

Related

Selecting or Updating with 2 group criteria for a max value on mysql [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 3 years ago.
I'm trying to figure out how to use group and max() on joins correctly, I'm doing some parsing of a moodle(Open source school software) mysql database. Students are allowed to retake the quizes indefinatly for this particular program, but I need to be able to update the course completion date to reflect the last time they took the test because a lot of other things depend on the completion fields.
The mdl_quiz_attempts table stores all attempts for all quizes, the userid will have many of the same entries, but the attempt number is not unique to the table, but instead unique to both the student AND the key for the row. Meaning students have multiple entries. On the mdl_course_modules table, The instance field is the key for the mdl_quiz table, and the mdl_course_modules_completion coursemoduleid field is the key for mdl_course_modules.
So what I want to do is this:
given a student id
UPDATE mdl_course_completion.timemodified to mdl_quize_attempts.timemodified
WHERE the row on mdl_quiz_attempts is the max attempt by userid for each quiz.(the quiz field on the quiz_attempts has to be looked up through in course module instance table to get the, instance id for course completion module id)
Here are example partial tables.
mdl_quiz_attempts
id quiz userid attempt timemodified
2 1 3 6 1365408901
6 1 4 1 1369873688
7 2 4 1 1369877532
8 7 4 1 1369881431
9 7 4 2 1369882897
12 5 4 1 1505165504
13 6 4 1 1369887643
17 8 4 1 1369958105
18 1 4 2 1374557701
22 7 4 3 1374639901
23 6 4 7 1374640202
24 5 4 2 1374639901
25 8 4 2 1374639901
26 2 4 2 1374639301
27 2 6 1 1376620469
29 2 12 1 1389915486
30 1 23 1 1390978667
31 1 23 2 1391030924
32 2 23 1 1392113103
33 2 23 2 1392696602
34 2 23 3 1392767435
35 7 12 1 1398914256
36 8 43 1 1405281193
37 1 50 1 1405522411
38 5 43 1 1505165504
mdl_course_modules
id course module instance section
3 2 9 2 3
5 2 17 2 4
7 2 17 3 5
8 2 17 4 6
9 2 17 5 7
10 2 17 6 8
11 2 17 7 9
12 2 17 8 10
13 2 17 9 11
14 2 17 10 12
15 2 17 11 13
25 2 16 1 14
26 2 23 1 4
28 2 7 1 14
30 4 9 4 26
42 4 23 3 33
45 4 23 6 38
46 4 23 7 37
47 4 23 8 36
48 4 23 9 35
49 4 23 10 32
50 4 23 11 34
51 5 9 5 27
53 5 23 12 43
55 5 23 13 44
mdl_quiz
id name
10 Unit 10 Quiz
11 Unit 2 Quiz
12 Unit 3 Quiz
13 Unit 5 Quiz
14 Unit 1 Quiz
15 Unit 8 Quiz
16 Unit 9 Quiz
17 Unit 7 Quiz
18 Unit 4 Quiz
mdl_course_modules_completion
id coursemoduleid userid completionstate viewed timemodified
14 25 2 0 1 0
15 25 6 0 1 0
67 25 4 1 1 1369873688
68 28 4 1 0 1369874483
69 192 4 1 0 1369875233
70 184 4 1 1 1369877532
Something like this ?
update mdl_course_modules_completion c
join mdl_quiz_attempts a on a.userid = c.userid
join (select max(attempt) max_attempts from mdl_quiz_attempts group by userid) max on max.max_attempts = a.attempt
set c.timemodified = a.timemodified
where c.userid = :<USER_ID>

MYSQL I am trying to return a value where I need to compare one value against the minimum value for the same field when grouped against another field

Basically I am trying to calculate shots received in golf for various four balls, here is my data:-
DatePlayed PlayerID HCap Groups Hole01 Hole02 Hole03 Shots
----------------------------------------------------------------------
2018-11-10 001 15 2 7 3 6
2018-11-10 004 20 1 7 4 6
2018-11-10 025 20 2 7 4 5
2018-11-10 047 17 1 8 3 6
2018-11-10 048 20 2 8 4 6
2018-11-10 056 17 1 6 3 5
2018-11-10 087 18 1 7 3 5
I want to retrieve the above lines with an additional column which is to be calculated depending on the value in the group column, which is the players (Handicap - (the lowest handicap in the group)) x .75
I can achieve it in a group by but need to aggregate everything, is there a way I can return the value as above?, here is query that returns the value:
SELECT
PlayerID,
MIN(Handicap),
MIN(Hole01) AS Hole01,
MIN(Hole02) AS Hole02,
MIN(Hole03) AS Hole03,
MIN(CourseID) AS CourseID,
Groups,
ROUND(
MIN((Handicap -
(SELECT MIN(Handicap) FROM Results AS t
WHERE DatePlayed='2018-11-10 00:00:00' AND t.Groups=Results.Groups)) *.75))
AS Shots
FROM
Results
WHERE
Results.DatePlayed='2018=11=10 00:00:00'
GROUP BY
DatePlayed, Groups, PlayerID
.
PlayerID MIN(Handicap)Hole01 Hole02 Hole03 CourseID Groups Shots
-----------------------------------------------------------------
4 20 7 4 6 1 1 2
47 17 8 3 6 1 1 0
56 17 6 3 5 1 1 0
87 18 7 3 5 1 1 1
1 15 7 3 6 1 2 0
25 20 7 4 5 1 2 4
48 20 8 4 6 1 2 4
Sorry about any formatting really couldn't see how to get my table in here, any help will be much appreciated, I am using the latest mysql from ubuntu 18.04
Not an answer; too long for a comment...
First off, I happily know nothing about golf, so what follows might not be optimal, but it must, at least, be a step in the right direction...
A normalized schema might look something like this...
rounds
round_id DatePlayed PlayerID HCap Groups
1 2018-11-10 1 15 2
2 2018-11-10 4 20 1
round_detail
round_id hole shots
1 1 7
1 2 3
1 3 6
2 1 7
2 2 4
2 3 6
Hi Guys I have found the solution, basically I need to drop the MIN immediately after the ROUND of the equation and therefore it does not need a Group By.
SELECT
PlayerID,
Handicap,
Hole01,
Hole02,
Hole03,
CourseID,
Groups,
ROUND((Handicap -
(SELECT MIN(Handicap) FROM Results AS t
WHERE DatePlayed='2018-11-10 00:00:00'
AND t.Groups=Results.Groups))
*.75) AS Shots
FROM
Results
WHERE
Results.DatePlayed='2018=11=10 00:00:00'

Get the latest 3 nodes foreach taxonomy term in one query

I am trying to setup a query to get the 3 most recent nodes (nid) foreach taxonomy term (tid). Is this possible to set up on one query?
Here are how my tables are set up (not displaying full table info)
taxonomy_index (Holds the taxonomy ids that are associated to a node)
nid tid
1 20
1 21
1 22
2 20
2 21
3 23
3 24
4 20
4 21
5 20
5 21
5 22
5 23
6 20
6 21
6 24
7 20
7 21
8 20
8 21
9 20
9 21
9 22
9 23
.....
node (node information)
nid title created
1 Article One 1105350260
2 Article Two 1105350259
3 Article Three 1105350261
4 Article Four 1105350280
5 Article Five 1105350290
6 Article Six 1105350290
.....
I'm envisioning a result like this if I wanted to see the latest 3 nodes for tids: 20, 21, 22:
tid nid
20 1
20 2
20 4
21 1
21 2
21 4
22 1
22 5
22 9
Is this possible in one query?
You can try follwing query:-
SELECT t.tid, t.nid
FROM taxonomy_index s
WHERE (SELECT COUNT(*)
FROM taxonomy_index f
WHERE f.tid = s.tid
AND f.nid <= s.nid
) <= 3;
here i have used <=3 #cond, in case if any tid dont have 3 records the it will fetch 2 or 1, whichever is more.

Finding continuous data from a table for months

I have a table which has month data in INT April=4, May=5 and so on. I want those records which have continuous data. My table is as follows. So if a record has discontinuous data, it should not be returned.
Continuous data means those records which having continuous four month. If records are there for 4,5,6,7 or 5,6,7,8 or 6,7,8,9 months then that record should come in result of a ID. If there records for a ID has 4,5,8,9 in month field this is discontinue data for me.
Initial query:
select ID, month from table1 where month in (4,5,6,7,8,9) group by month;
Initial Data:
PK ID month value
1 1 4 400
2 1 5 200
3 1 6 300
4 1 7 400
5 2 5 400
6 2 6 200
7 2 7 100
8 2 8 400
9 3 4 200
10 4 5 800
11 5 6 800
12 5 7 100
13 5 8 700
14 5 9 900
15 6 4 100
16 6 5 200
17 6 8 500
18 6 9 600
Result:
PK ID month value
1 1 4 400
2 1 5 200
3 1 6 300
4 1 7 400
5 2 5 400
6 2 6 200
7 2 7 100
8 2 8 400
11 5 6 800
12 5 7 100
13 5 8 700
14 5 9 900
My database is MySQL.
I have months from April(4) to Sept(9)
Try the below query this will work I hope!
SELECT ID, Month
FROM table1 WHERE month in (4,5,6,7,8,9)
GROUP BY ID, Month
HAVING count(ID)>3
kindly let me know this is working or not

Puzzle: find number in a given row and column in 2-dimensional array where no number may occur twice

We have a two-dimensional array with the number 0 in the upper left corner. The rest of the array is then filled with numbers so that each index contains the smallest positive integer possible that already exists neither on the same row or column.
Example:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14 17 16
2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13 18 19
3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12 19 18
4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11 20 21
5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10 21 20
6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9 22 23
7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23 22
8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 24 25
9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6 25 24
10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5 26 27
11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4 27 26
12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3 28 29
13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2 29 28
14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 30 31
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 31 30
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 0 1
17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30 1 0
Given the row and the column in such array, I need to be able to find the number in the specified index in less than one second on a relatively new desktop PC (for row and column less than a million). My brute-force attempts so far have been so futile that it's clearly not the way I want to go with this. Presumably there must be a way to find out the number in question, in linear time (?), that doesn't require computing all the preceding numbers in the array.
Observation shows that the operator is the bitwise XOR (represent each operand as a binary number, XOR together the corresponding bits, read as binary).
Now on to prove it is the XOR:
Since the XOR with one argument fixed is a bijection on the other argument, the "that exists neither on the same row or column" is satisfied.
Now it just suffices to prove the "smallest" part, namely that any lower value already occurs if we reduce either operand:
foreach A >= 0, B >= 0, F >= 0:
(A xor B > F) => (exists D: D xor B = F) or (exists E: A xor E = F)
or equivalently
foreach 0 <= A, 0 <= B, 0 <= F < (A XOR B)
(exists D: D xor B = F) or (exists E: A xor E = F)
Note that we are no longer concerned about our operator, we're proving the minimality of XOR.
Define C = A xor B
if A = 0, B = 0, then minimality is satisfied.
Now, if A and B have the same magnitude (the same bit length), then clearing the top bit of both will not change C. Clearing the top bit is a translation towards the origin in the matrix, so if a smaller value exists above or to the left after translation, it is at the same relative position before the translation.
A and B must have a different magnitude to be a counter-example. XOR (as well as the operator under consideration) are symmetric, so assume A > B.
If F is of greater magnitude than A, then it's not smaller, and thus it's not a counter-example.
If F has the same magnitude as A, then clear the highest bit in A and in F. This is a translation in the table. It changes the values, but not their ordering, so if a smaller value exists above or to the left after translation, it is at the same relative position before the translation.
If F has a smaller magnitude than A, then, by the pigeonhole principle and the properties of XOR, there exists a D with a smaller magnitude than A such that D xor B = F.
summary: The proof that XOR satisfies the conditions imposed onto the solution follows from the symmetries of XOR, its magnitude-preserving properties and its bijection properties. We can find each smaller element than A xor B by reducing A, B and the challenge until they're all zero or of different magnitude (at which point we apply the pigeonhole principle to prove the challenge can be countered without actually countering it).