2013-08-02 17 views
9

Chciałbym dodać punkty za najwyższe oceny. mój stół jestDodaj punkty na najwyższych ocenach

enter image description here

Próbuję dać punkty na najwyższych 3 znaków studenta. 1st najwyższe noty idzie do 5 punktów, 2. najwyższe noty idzie do 3 znaków i 3 najwyższa idzie do 1 znaków

Używam tego kodu do wybierania najwyższe noty,

select t1.ID, t1.Name, t1.Section, t1.Marks from myTable t1 join 
(select Section, substring_index(group_concat 
(distinct Marks order by Marks desc), ',', 3) as Marks3 
from myTable group by Section) tsum on t1.Section = tsum.Section 
    and find_in_set(t1.Marks, tsum.Marks3) > 0 ORDER BY Section, Marks DESC, ID Desc 

Chciałbym dodać 5 punktów dla 1 najwyższych wartości, 3 punkty dla 2 najwyższych i 1 dla 3. najwyższych ocen. W przypadku uczniów mogą pojawiać się duplikaty znaków.

Zapoznaj http://www.sqlfiddle.com/#!2/dca0c/1

Więc moje ostatnie wyjście

enter image description here

Proszę mi pomóc ..

+0

+1 za miły pytanie i za wysiłek już umieścić na nim –

Odpowiedz

1
select t1.ID, t1.Name, t1.Section, t1.Marks, 
case ((SELECT COUNT(distinct MARKS) FROM students t2 WHERE t2.marks > t1.marks 
and t1.Section = t2.Section) +1) when 1 then 5 when 2 then 3 else 1 end as Points 
from students t1 join 
    (select Section, substring_index(group_concat(distinct Marks order by Marks desc), ',', 3) as Marks3 
     from students 
     group by Section 
    ) tsum 
    on t1.Section = tsum.Section and 
     find_in_set(t1.Marks, tsum.Marks3) > 0 
ORDER BY Section, Marks DESC, ID ASC; 

wyjściowa: Dokładnie jak popyt. ;)

ID NAME SECTION MARKS POINTS 
1 S1 class1 55 5 
7 S7 class1 32 3 
3 S3 class1 25 1 
10 S10 class2 78 5 
14 S14 class2 78 5 
6 S6 class2 66 3 
2 S2 class2 33 1 
13 S13 class2 33 1 
4 S4 class3 65 5 
11 S11 class3 65 5 
5 S5 class3 43 3 
12 S12 class3 43 3 
15 S15 class3 25 1 

fiddle

3

AKTUALIZACJA Można zrobić to tak

SELECT id, name, section, marks, 
     CASE rank WHEN 1 THEN 5 
       WHEN 2 THEN 3 
       WHEN 3 THEN 1 
       ELSE 0 
     END points 
    FROM 
(
    SELECT s.*, @n := IF(@s = section, IF(@m = marks, @n, @n + 1), 1) rank, @m := marks, @s := section 
    FROM students s, (SELECT @n := 0) i 
    ORDER BY section, marks DESC 
) q 
HAVING points > 0 

wyjściowa:

 
| ID | NAME | SECTION | MARKS | POINTS | 
---------------------------------------- 
| 1 | S1 | class1 | 55 |  5 | 
| 7 | S7 | class1 | 32 |  3 | 
| 3 | S3 | class1 | 25 |  1 | 
| 10 | S10 | class2 | 78 |  5 | 
| 14 | S14 | class2 | 78 |  5 | 
| 6 | S6 | class2 | 66 |  3 | 
| 2 | S2 | class2 | 33 |  1 | 
| 13 | S13 | class2 | 33 |  1 | 
| 4 | S4 | class3 | 65 |  5 | 
| 11 | S11 | class3 | 65 |  5 | 
| 5 | S5 | class3 | 43 |  3 | 
| 12 | S12 | class3 | 43 |  3 | 
| 15 | S15 | class3 | 25 |  1 | 

Oto SQLFiddle demo

+0

Wszelkie zapytania filtr dostępny odfiltrować rekordy zawierające „0” punkty? –

+0

grał na skrzypcach. Nie dostanie się jednak. –

+0

@PraveenPrasannan Zobacz zaktualizowaną odpowiedź i sqlfiddle dla odfiltrowania wierszy z 0 punktów. – peterm

Powiązane problemy