my table
+------+-------+---------+-------+--------+
| Name | Group1| Section | Marks | Points |
+------+-------+---------+-------+--------+
| S1 | G1
I have found a solution to list the groups with their allocated points but I was having a hard time storing the result back into myTable, i.e. doing an UPDATE on the table. I managed at last (see bottom of post!!).
But first, here is the group score-table generator (group overview):
SELECT mg,ms,mm,
CASE WHEN @s=ms THEN
CASE WHEN @m=mm THEN @i
WHEN @i>2 THEN @i:=@i-2
ELSE null END
ELSE @i:=5 END pt,
@g:=mg gr,@s:=ms,@m:=mm
FROM (
SELECT group1 mg,section ms,max(marks) mm FROM mytable
WHERE group1>''
GROUP BY group1,section
) m
ORDER BY ms,mm desc,mg
http://sqlfiddle.com/#!2/bea2a2/1
It gives me this list:
| MG | MS | MM | PT | GR | @S:=MS | @M:=MM |
------------------------------------------------------
| G1 | class1 | 55 | 5 | G1 | class1 | 55 |
| G10 | class1 | 55 | 5 | G10 | class1 | 55 |
| G5 | class1 | 32 | 3 | G5 | class1 | 32 |
| G8 | class1 | 22 | 1 | G8 | class1 | 22 |
| G9 | class1 | 20 | (null) | G9 | class1 | 20 |
| G66 | class2 | 66 | 5 | G66 | class2 | 66 |
| G88 | class2 | 65 | 3 | G88 | class2 | 65 |
| G4 | class2 | 60 | 1 | G4 | class2 | 60 |
| G55 | class2 | 33 | (null) | G55 | class2 | 33 |
I am back (26.08.2013, after having asked for help myself, see here) and can now provide the full answer:
SET @s:=@m:=@i:='a'; -- variables *MUST* be "declared" in some
-- way, otherwise UPDATE will not work!
UPDATE mytable INNER JOIN
(SELECT mg,ms,mm,
CASE WHEN @s=ms THEN
CASE WHEN @m=mm THEN @i
WHEN @i>2 THEN @i:=@i-2
ELSE null END
ELSE @i:=5 END pt,
@s:=ms,@m:=mm
FROM (
SELECT group1 mg,section ms,max(marks) mm FROM mytable
WHERE group1>''
GROUP BY group1,section
) m
ORDER BY ms,mm desc,mg
) t ON mg=group1 AND ms=section AND mm=marks
SET Points=pt
see here http://sqlfiddle.com/#!2/bb7f2
And finally - off-topic:
Dear user @user2594154, why have you bombarded this board with the same question 8 times?!?
It would be much more helpful for everybody if you had kept your question in one post, explaining exactly what you want and also what you have tried yourself (!!). Then, in the course of the answering process, it is possible to edit it, making it more precise. New questions should only be posted, if their subject is actually different, see here.
No hard feelings - I learnt a lot in the process of solving this problem of yours. ;-)