Let's say I write a SELECT statement in MYSQL(version MySQL 8.0) which fetches "n" records.
I want this "n" records be split approximately equally by 3 and additionally need to make sure that 10% of these n records are shared by the 3 parts that it was divided into.
Is it possible to do this in the select statement itself or do I need to write a function to perform this operation.
Example:
My select query fetches as below:
ID Name Age
1 ABC 22
2 XYZ 23
3 awe 22
4 wer 23
5 per 23
6 mnm 24
7 lmk 23
8 uyt 22
9 prt 23
10 ler 26
I now need it split into let's say 3 and 10% should be shared by the 3 parts that it has been split to (since it's 10 records, in this example 1 record should be shared by the three sets)
Set 1:
1 ABC 22
2 XYZ 23
3 awe 22
4 wer 23
Set 2:
4 wer 23
5 per 23
6 mnm 24
7 lmk 23
Set 3:
4 wer 23
8 uyt 22
9 prt 23
10 ler 26
Related
I have two tables which both have column acc_manager and would like to update both tables where the value for acc_manager is 34 to be 68 but I only wish for half of the values of my client table to be updated including related data in my company table.
In my example tables below, there are discrepancies such as a company having an account manager but a client of the same company may have a different account manager.
client
id name companyid acc_manager
1 name1 1 12
2 name2 2 23
3 name3 2 34
4 name4 3 34
5 name5 3 34
6 name6 4 34
7 name7 5 12
8 name8 4 34
9 name9 6 34
company
id name acc_manager
1 company1 12
2 company2 23
3 company3 34
4 company4 34
5 company5 12
6 company6 0
I've tried to update using UPDATE and LIMIT like so
UPDATE client, company
SET client.acc_manager = 68, company.acc_manager = 68
WHERE company.id = client.companyid
AND client.acc_manager = 34
AND company.acc_manager = 34
LIMIT 55
but this resulted in the following error
Incorrect usage of UPDATE and LIMIT
so I tried updating the values of just one table using both tables in the WHERE clause but this outputted another error.
Is there any way I can update both tables, updating half of the items that match a criteria in the client table, and any related rows in the company table?
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>
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'
i have table with 3 columns , now how find value if it appears next 3 times immediately
date tran_val name
1 23mar 22 mark
2 24mar 22 mark
3 25mar 22 mark
4 26mar 22 mark
5 27mar 22 mark
6 28jan 99 john
7 29jan 99 john
8 30jan 99 john
9 31jan 99 john
output is like
date tran_val name
1 23mar 22 mark
2 24mar 22 mark
6 28jan 99 john
i.e 1st trnas_value appears in next 3 consecutive times (repeaded 4 times)
and 2nd and 6th also rows also repeaded the same..
thanks in advance
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.