Sum of marks in one table

moonvader

I have MySQL table with structure like

lesson_id | task_id | user_id | mark

There can be many users and tasks on each lesson

I want to get 2 results :

  1. sum of all marks for each user starting with big values (DESC order)
  2. sum of all marks for one specified lesson for each user starting with big values (DESC order)

Expected result 1.

user_1 500
user_3 423
user_2 100
user_4 92

Expected result 2.

user_id |  mark | lesson_id 
user_3  |  423  |  2
user_2  |  100  |  2
user_4  |   92  |  2 
Liya Tansky

I agree with scaisEdge about the 1st question for the 2nd question you need to add filtering by lesson to the same query:

select user_id, sum(mark) sum_of_mark 
from your_table 
group by user_id 
where lesson_id = @lesson 
order by sum_of_mark DESC;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

groupby, sum and count to one table

From Dev

groupby, sum and count to one table

From Dev

Mysql : JOIN 2 table , one of the SUM is not correct

From Dev

Get the sum of all rows in one table

From Dev

get sum of multiple values by months in one table

From Dev

Sum one table and update other with result, or just do `sum` on select?

From Dev

Mysql group by and having & sum of one column from table one

From Dev

Joining row from one table with the sum value from another table

From Dev

Calculating the sum of the quantities of one table based on dates in another table in sql

From Dev

Group by one table and sum from another table in Linq

From Dev

Set two values in one table based on sum of a column in another table

From Dev

Getting marks contribution based on sum not working

From Dev

MySQL & PHP: Sum(marks) along with where clause

From Dev

Mysql from sum one table column subtract sum of another table column

From Dev

postgres sum different table columns from many to one joined data

From Dev

How to Get Sum of One Column Based On Other Table in Sql Server

From Dev

Generate SUM For Foreign Key on one table and append to another in MySQL

From Dev

How to SUM from another table in SQL in one view?

From Dev

How to create table having one column as sum of other columns?

From Dev

Fill data of one row using other table column sum in mysql

From Dev

SQL Server Sum multiple rows into one - no temp table

From Dev

How to use SUM() and merge tables into one table with SQL?

From Dev

How to use SUM() and merge tables into one table with SQL

From Dev

Get sum of more than one table columns with a single query

From Dev

How to sum a specific row from one table to another in SQL Server

From Dev

Sum fixed number of cells of one column in data.table

From Dev

Outlook sometimes marks folder with one unread message

From Dev

Adding multiple marks in one line in Vim?

From Dev

R: read.table with quotation marks

Related Related

  1. 1

    groupby, sum and count to one table

  2. 2

    groupby, sum and count to one table

  3. 3

    Mysql : JOIN 2 table , one of the SUM is not correct

  4. 4

    Get the sum of all rows in one table

  5. 5

    get sum of multiple values by months in one table

  6. 6

    Sum one table and update other with result, or just do `sum` on select?

  7. 7

    Mysql group by and having & sum of one column from table one

  8. 8

    Joining row from one table with the sum value from another table

  9. 9

    Calculating the sum of the quantities of one table based on dates in another table in sql

  10. 10

    Group by one table and sum from another table in Linq

  11. 11

    Set two values in one table based on sum of a column in another table

  12. 12

    Getting marks contribution based on sum not working

  13. 13

    MySQL & PHP: Sum(marks) along with where clause

  14. 14

    Mysql from sum one table column subtract sum of another table column

  15. 15

    postgres sum different table columns from many to one joined data

  16. 16

    How to Get Sum of One Column Based On Other Table in Sql Server

  17. 17

    Generate SUM For Foreign Key on one table and append to another in MySQL

  18. 18

    How to SUM from another table in SQL in one view?

  19. 19

    How to create table having one column as sum of other columns?

  20. 20

    Fill data of one row using other table column sum in mysql

  21. 21

    SQL Server Sum multiple rows into one - no temp table

  22. 22

    How to use SUM() and merge tables into one table with SQL?

  23. 23

    How to use SUM() and merge tables into one table with SQL

  24. 24

    Get sum of more than one table columns with a single query

  25. 25

    How to sum a specific row from one table to another in SQL Server

  26. 26

    Sum fixed number of cells of one column in data.table

  27. 27

    Outlook sometimes marks folder with one unread message

  28. 28

    Adding multiple marks in one line in Vim?

  29. 29

    R: read.table with quotation marks

HotTag

Archive