I have a data set:
sid name sub marks subid
11 kittu eng 55 1
11 kittu math 0 2
11 kittu sci 0 3
12 bunnu eng 0 1
12 bannu math 44 2
12 bannu sci 0 3
13 siva eng 0 1
13 siva math 0 2
13 siva sci 88 3
and I want to get output like this (which have consecutive zero marks):
11 kittu
13 siva
SELECT a row from T WHERE exists a row another with same sid and a lower subid sequentially and both rows have zero marks.
Then Do a DISTINCT just in case there are more than qualifying condition so you don't get duplicates.
SELECT DISTINCT sid,name
FROM t
WHERE EXISTS (SELECT 1 FROM T T2
WHERE T2.marks = 0
AND t.marks = 0
AND T2.sid = T.sid
AND T2.subid = t.subid + 1)
sqlfiddle
Related
I have a table as follows:
ID PID TID CID DID IsTrue
1 43 1 2 621 0
2 43 1 2 621 1
3 45 2 3 621 1
4 46 2 3 621 0
and I need to find the count of PID,CID and with the where condition DID=621 along with the other fields.
how can I do that?
Thanks.
Try this
Select count(pid) from table where did = 121 group by pid.
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;
Say I have a table called students
idStudent Name
1 Billy
2 Mariah
3 Chris
4 Mark
5 Sarah
and another table called tests
idTest score student_idstudent
1 50 1
2 100 1
3 90 2
4 100 3
5 45 4
is it possible to use a combination of a join and avg() to get a result like
idStudent avg_test
1 75
2 90
3 100
4 45
5 0
SELECT s.idStudent,
AVG(COALESCE(t.score, 0)) AS avg_test
FROM students s
LEFT JOIN tests t
ON s.idStudent = t.student_idStudent
GROUP BY s.idStudent
I need to combine the results of
select max(mark), exam_seq, subject_seq from mark where section_seq = 1
group by exam_seq, subject_seq;
and
select mark, exam_seq, subject_seq from mark where stu_seq = 1
group by exam_seq, subject_seq;
in to one result set.
Both the queries will return the same number of results. First query gets the max mark for the given Class-Section group by Exams and Subjects; Second query gets the individual marks for a Student group by Exams and Subjects.
Based on other posted answers to similar queries, I tried
select max(mark), exam_seq, subject_seq from mark where section_seq = 1
group by exam_seq, subject_seq
union all
select mark, exam_seq, subject_seq from mark where stu_seq = 1
group by exam_seq, subject_seq
but it produces the result one below the other.
What I need is a result with
4 columns: mark, max(mark), exam_seq, subject_seq.
'mark' from second query and
'max(mark)' from first query
Please note that the where conditions of the two sqls are different (based on two different fields). Please suggest. Thanks.
Here is the sample data for 1 Class-Section, 2 Exams, 5 Subjects and 2 Students:
section_seq exam_seq subject_seq stu_seq mark
1 1 1 1 65
1 1 1 2 56
1 1 2 1 65
1 1 2 2 56
1 1 3 1 56
1 1 3 2 65
1 1 4 1 99
1 1 4 2 88
1 1 5 1 55
1 1 5 2 66
1 2 1 1 65
1 2 1 2 56
1 2 2 1 77
1 2 2 2 68
1 2 3 1 99
1 2 3 2 9
1 2 4 1 88
1 2 4 2 45
1 2 5 1 66
1 2 5 2 66
Im assuming you can join on your group bys, so you can acheive that with a subquery and GROUP_CONCAT
SELECT allMarks.mark, max(mark), exam_seq, subject_seq from mark
LEFT JOIN(
SELECT GROUP_CONCAT(mark) as mark
FROM mark
WHERE stu_seq = 1
GROUP BY exam_seq, subject_seq
) allMarks
ON allMarks.exam_seq = mark.exam_seq
AND allMarks.subject_seq= mark.subject_seq
WHERE mark.section_seq = 1
GROUP BY mark.exam_seq, mark.subject_seq
If we had some sample data, we could advise on that and check the structure is correct.
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.