What I'm trying to do is to select a specific amount of tickets (max 2) and every person that has the sum of the number of tickets less of 3 and the valid field has to be != 'e'
I have this table:
ID
id_person
nr_tickets
valid
1
220
1
s
2
220
1
s
3
330
2
s
4
330
1
e
5
331
1
s
6
220
2
s
7
441
1
s
8
442
2
s
9
443
1
s
10
444
1
s
11
445
2
s
Here is what I did:
SELECT m.id, m.id_person, m.nr_tickets, m.valid
FROM table m
JOIN table m1 ON m1.id <= m.id
WHERE m.nr_tickets > 0
GROUP BY m.id
HAVING SUM(case when m.valid != 'e' then m1.nr_tickets end) <= 10
This query gives me
ID
id_person
nr_tickets
valid
1
220
1
s
2
220
1
s
3
330
2
s
5
331
1
s
6
220
2
s
7
441
1
s
8
442
2
s
As you can see the query it's almost right, the thing is that the person 220 in the results has the sum of the tickets is greater than 2.
What I'm trying to achieve is to bypass the ID 6, and to have instead the ID 9
select `id`,`id_person`, sum(`nr_tickets`) as `nr_tickets`, `valid`
from `test`
group by `id_person`
having sum(`nr_tickets`) < 3 and `valid`!="e"
Output:
id id_person nr_tickets valid
5 331 1 s
7 441 1 s
8 442 2 s
9 443 1 s
10 444 1 s
11 445 2 s
Related
WEEK STUDENT CLASS TEST SCORE
1 1 A 1 93
1 1 A 2 97
1 1 B 1 72
1 1 B 2 68
1 1 C 1 93
1 1 C 2 51
1 1 H 1 19
1 2 A 1 88
1 2 A 2 56
1 2 B 1 53
1 2 B 2 79
1 2 C 1 69
1 2 C 2 90
1 2 H 1 61
1 3 A 1 74
1 3 A 2 50
1 3 B 1 76
1 3 B 2 97
1 3 C 1 55
1 3 C 2 63
1 3 H 1 63
2 1 A 1 59
2 1 A 2 68
2 1 B 1 77
2 1 B 2 80
2 1 C 1 52
2 1 C 2 94
2 1 H 1 74
2 2 A 1 64
2 2 A 2 74
2 2 B 1 92
2 2 B 2 98
2 2 C 1 89
2 2 C 2 84
2 2 H 1 54
2 3 A 1 51
2 3 A 2 82
2 3 B 1 86
2 3 B 2 51
2 3 C 1 90
2 3 C 2 72
2 3 H 1 86
I wish to group by STUDENT and WEEK and find the MAXIMUM(SCORE) value when TEST = 1. Then I wish to add the corresponding rows for CLASS and also the score for TEST = 2 based to get this:
WEEK STUDENT CLASS TEST1 TEST2
1 1 A 93 97
2 1 A 88 56
1 2 B 76 97
2 2 B 77 80
1 3 B 92 98
2 3 C 90 72
This is what I try but in SQL I am no able to SELECT columns which I don't group by
SELECT STUDENT, WEEK, CLASS, MAX(SCORE)
FROM DATA
WHERE TEST = 1
GROUP BY (STUDENT, WEEK)
but I do not find a solution that works.
Write a subquery that gets the highest score for each week and student on test 1. Join that with the table to get the rest of the row for that same score.
Then join that with the table again to get the row for the same student, week, and class, but with test = 2.
SELECT t1.week, t1.student, t1.class, t1.score AS test1, t3.score AS test2
FROM yourTable AS t1
JOIN (
SELECT week, student, MAX(score) AS score
FROM yourTable
WHERE test = 1
GROUP BY week, student
) AS t2 ON t1.week = t2.week AND t1.student = t2.student AND t1.score = t2.score
JOIN yourTable AS t3 ON t3.week = t1.week AND t3.student = t1.student AND t3.class = t1.class
WHERE t1.test = 1 AND t3.test = 2
ORDER BY student, week
DEMO
I have 2 table name table 'hasil' and table 'kat_soal' to join and give rank on each 'KatID' field on 'hasil' table..
here is my hasil table :
HasilID KatID UserID JBenar JSalah Nilai
15 1 1000 2 1 66.66666666666666
16 3 1000 2 0 100
17 1 1001 1 2 33.33333333333333
18 3 1001 1 1 50
19 1 1002 3 0 90
20 3 1002 2 0 80
and there is my kat_soal table
KatID Kategori Lama
1 IPA 30
2 IPS 30
3 Matematika 30
4 Bahasa Indonesia 20
5 Bahasa Inggris 20
this my query generate rank:
SELECT a.KatID,a.UserID,b.Kategori,c.Nama,a.JBenar,a.JSalah,ROUND(a.Nilai,2) as Nilai,
FIND_IN_SET( a.Nilai, l.list) AS rank
from hasil a
JOIN kat_soal b
ON a.KatID = b.KatID
JOIN datauser c
ON a.UserID=c.UserID
CROSS JOIN
(SELECT GROUP_CONCAT( a2.Nilai ORDER BY a2.Nilai DESC ) as list
FROM hasil a2) l
WHERE a.KatID='1'
ORDER BY a.Nilai DESC;
my result
//FOR KatID=1
KatID UserID Kategori Nama JBenar JSalah Nilai rank
1 1002 IPA ratam 3 0 90.00 2
1 1000 IPA Tarsan 2 1 66.67 4
1 1001 IPA wisnu 1 2 33.33 6
//FOR KatID=3
3 1000 Matematika Tarsan 2 0 100.00 1
3 1002 Matematika ratam 2 0 80.00 3
3 1001 Matematika wisnu 1 1 50.00 5
My Expected Result
//FOR KatID=1
KatID UserID Kategori Nama JBenar JSalah Nilai rank
1 1002 IPA ratam 3 0 90.00 1
1 1000 IPA Tarsan 2 1 66.67 2
1 1001 IPA wisnu 1 2 33.33 3
//FOR KatID=3
3 1000 Matematika Tarsan 2 0 100.00 1
3 1002 Matematika ratam 2 0 80.00 2
3 1001 Matematika wisnu 1 1 50.00 3
anyone can help me ?
Good example to solve the issue is by looking at: http://www.fromdual.com/ranking-mysql-results .
You are making this a bit complicated: First You take the value, making the value a string, then "finding position in string".
From the example it should be completely ok if it is done as (untested):
SET #rank=0;
SELECT a.KatID,a.UserID,b.Kategori,c.Nama,a.JBenar,a.JSalah,ROUND(a.Nilai,2) as Nilai,
#rank:=#rank+1 AS rank
from hasil a
JOIN kat_soal b
ON a.KatID = b.KatID
JOIN datauser c
ON a.UserID=c.UserID
WHERE a.KatID='1'
ORDER BY rank;
EDIT: Changed ordering - You are expecting to by ordered by rank in the final.
Below is the script without using table datauser for any1 to test:
SET #rank=0;
SELECT a.KatID,a.UserID,b.Kategori,a.JBenar,a.JSalah,ROUND(a.Nilai,2) as Nilai,
#rank:=#rank+1 AS rank
from hasil a
JOIN kat_soal b
ON a.KatID = b.KatID
WHERE a.KatID='1'
ORDER BY rank;
First off, for categorization purposes, could someone figure out a better name to index this as?
Secondly, I have a table and I want to grab the username from another table to two columns on the same table.
SELECT ignite_board.*,username AS 'name'
FROM ignite_board,user_info
WHERE bnumber = 3
AND ignite_board.uid = user_info.id
ORDER BY POSITION
Gets me this:
ID bnumber position UID qual time following name
1176 3 1 442 2 1382045726 440 bb1
1177 3 2 445 2 1382045936 442 bb4
1181 3 3 450 2 1382050220 449 bb6
1178 3 4 446 2 1382050371 439 aa2
1209 3 5 466 1 1382050130 450 bb8
1212 3 6 469 2 1382050502 467 bb10
1210 3 7 467 1 1382050172 466 bb9
1232 3 8 475 1 1382050415 446 aa7
1180 3 9 444 0 1382045690 445 bb3
1214 3 10 471 0 1382050220 466 bb11
1233 3 11 476 0 1382050415 475 aa8
1179 3 12 441 0 1382045562 475 aa1
1182 3 13 452 0 1382046032 448 aa6
1216 3 14 473 0 1382050272 469 bb12
1234 3 15 477 0 1382050502 469 bb16
1271 3 16 454 0 1382306814 442 bb7
But what would I need to get the username and the Following username (sorta like this)
SELECT ignite_board.*,username AS 'name',username AS 'following'
FROM ignite_board,user_info
WHERE bnumber = 3
AND ignite_board.uid = user_info.id
AND ignite_board.following = user_info.id
ORDER BY POSITION
SELECT b.*
, u.username name
, f.username following
FROM ignite_board b
JOIN user_info u
ON u.id = b.uid
JOIN user_info f
ON f.id = b.following
WHERE bnumber = 3
ORDER
BY position;
You have to join the user_info table twice, and give a different alias to each instance:
SELECT ignite_board.*,u.username AS 'name',f.username AS 'following'
FROM ignite_board,user_info u, user_info f
WHERE bnumber = 3
AND ignite_board.uid = u.id
AND ignite_board.following = f.id
ORDER BY POSITION
I Have a marksheet table like:
ID STUDENT_ID Branch_id Class_id Exam_id Subject_id Numbers Date
1 653 5 1 1 8 60 2012-01-01
2 653 5 1 1 9 40 2012-01-01
3 653 5 1 1 10 80 2012-01-01
4 653 5 1 1 11 50 2012-01-01
5 653 5 1 1 12 65 2012-01-01
6 653 5 1 1 13 33 2012-01-01
7 653 5 1 1 15 86 2012-01-01
8 222 5 1 1 8 100 2012-01-01
9 222 5 1 1 9 80 2012-01-01
10 222 5 1 1 10 92 2012-01-01
11 222 5 1 1 11 50 2012-01-01
12 222 5 1 1 12 65 2012-01-01
13 222 5 1 1 13 33 2012-01-01
7 222 5 1 1 15 86 2012-01-01
I want to get rank I got answer by this question
Also when I fetched all class result I use pivot query:
SELECT stu_id, sum(numbers) AS total, branch_id, depart_id, class_id,
SUM( IF( subject_id =1, numbers, 0 ) ) AS MAth,
SUM( IF( subject_id =2, numbers, 0 ) ) AS Eng,
SUM( IF( subject_id =3, numbers, 0 ) ) AS Science
FROM marksheet where branch_id = 1 AND depart_id = 1
AND class_id = 1 GROUP BY stu_id ORDER BY total DESC
I want to get rank in my class query (pivot query)? And I want to count how many students on first position and how many on second and third?
Required Data sample:
ID Name Math English Science Total Percent Position Rank
Any one help?
I think what you need to do is create a second table with grade boundaries that are being referenced so for instance :
ID grade start_boundry end_boundry
1 A 60 100
ect..
then create a join between the tables and then do a WHERE statement between the Numbers and the start/ end boundries
so ->
SELECT grade FROM boundries_table RIGHT JOIN sudent_table
WHERE boundries_table.start_boundry < student_table.numbers
AND boundries_table.end_boundry > student_table.numbers
i think that should work if my MySQL memory serves, just modify the table to how you need it to run and it should work for how you need it.
I have a result table like:
ID STUDENT_ID Branch_id Class_id Exam_id Subject_id Numbers Date
1 653 5 1 1 8 60 2012-01-01
2 653 5 1 1 9 40 2012-01-01
3 653 5 1 1 10 80 2012-01-01
4 653 5 1 1 11 50 2012-01-01
5 653 5 1 1 12 65 2012-01-01
6 653 5 1 1 13 33 2012-01-01
7 653 5 1 1 15 86 2012-01-01
8 222 5 1 1 8 100 2012-01-01
9 222 5 1 1 9 80 2012-01-01
10 222 5 1 1 10 92 2012-01-01
11 222 5 1 1 11 50 2012-01-01
12 222 5 1 1 12 65 2012-01-01
13 222 5 1 1 13 33 2012-01-01
7 222 5 1 1 15 86 2012-01-01
My Desire Result like:
Student_ID Math English Science Total Rank
1 90 89 88 267 1
2 90 89 88 267 1
3 58 45 98 201 2
I want to get student rank by this method:
Reference Link
SET #rank = 0, #prev_val = NULL;
SELECT rank, correct FROM
(
SELECT
#rank := IF(#prev_val=correct,#rank,#rank+1) AS rank,
#prev_val := correct AS correct, uid
FROM quiz_user
ORDER BY correct DESC
)as result WHERE uid=xxxxxxxxxxxx
This query what I need only difference between table structure the author of post assign a rank on correct column and I need to assign rank on numbers SUM(numbers) column after sum all numbers.
Please Help.
Try this:
SELECT STUDENT_ID, Numbers, IF(#marks=(#marks:=Numbers), #auto, #auto:=#auto+1) rank
FROM (SELECT STUDENT_ID, SUM(Numbers) Numbers
FROM quiz_user
GROUP BY STUDENT_ID
ORDER BY Numbers DESC, STUDENT_ID
) AS A, (SELECT #auto:=0, #marks:=0) AS B;