MySql query : Get sum of N rows where N is defined in another column value

Ammar Hayder Khan

I have the following table,

 id  |   add_card_id |   rate | category |  category_count |    counts | total
7597 |    723        |    3   |   7      |      1          |     300   | 108
7596 |    723        |    3   |   9      |      1          |     500   | 180
7594 |    723        |    3   |   11     |      1          |     400   | 240
7593 |    723        |    5   |   10     |      2          |     400   | 240
7593 |    723        |    5   |   13     |      2          |     400   | 240

I want to get sum of total(column name) where rate is same but sum include only nth no. of rows having max total, nth is defined by category_count (column name).

e.g the rate of first three column is 3 and category_ count is 1 so sum (total) first three rows is 240 because 240 > 180 > 108 and in the case of last two rows rate is 5 and category_count is 2 so 240 +240= 480 and i want 720= 240(from first three rows) + 480 (from last two rows).

I have tried the following query

SELECT SUM(total) 
FROM (SELECT MAX(total) 
      FROM tableName 
      GROUP BY rate, category_count) as newTable

it's giving me 240+ 240 so how can I give the limit by category_count(column)?

StuartLC

You can simulate ROW_NUMBER and PARTITION BY in MySql by dynamically assigning variables

SELECT rate, SUM(total) 
FROM 
(
  SELECT total, rate, category_count, 
         -- Row num counter, resets at each different rate
         @row_num := IF(@prev_rate=rate, @row_num+1, 1) AS RowNum, 
         @prev_rate := rate -- Track to see if we are in the same rate
  FROM tableName,
     (SELECT @row_num := 1) x, -- Set initial value for @row_num and @prev_rate
     (SELECT @prev_rate := '') y
  -- Important, must keep rates together, then order by your requirement
  ORDER BY rate ASC, total DESC 
) ranked
WHERE ranked.RowNum <= ranked.category_count -- Your requirement of TOP N
GROUP BY rate;

The above returns

Rate Total
3    240
5    480

And if you do the SELECT SUM(total) then drop the GROUP BY you'll get 720 as you want.

SqlFiddle here

Edit

It seems rate is defined as (I'd assumed an integer from your sample data)

`rate` varchar(X) COLLATE utf8_unicode_ci

Change the one line:

 (SELECT @prev_rate := '' COLLATE utf8_unicode_ci) y

Which will set the temporary tracking variable to the same type as your column

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL Select Query: SUM() rows with a distinct value, from another column

From Dev

MySql Query to sum the rows and get the max value

From Dev

Get the rows where column/field sum equals a certain value

From Dev

MySQL get count of all rows in another table where the value equals column in current table

From Dev

mysql query select multiple rows where column value is same

From Dev

SQL Query: Get rows where value of certain column is maximum

From Dev

How to get the first N rows where the sum is ≥ X?

From Dev

How to get the first N rows where the sum is ≥ X?

From Dev

MySQL query to get the sum of a column

From Dev

Get 'balanced' column value as 'Y' if both rows has same amt and 'N' if not in SQL query

From Dev

Get rows where one column is unique and another column is the lowest value relative to the unique column

From Dev

Get a table with a value as key and a count or sum of another column, only of rows with that value

From Dev

Pandas - New column based on the value of another column N rows back, when N is stored in a column

From Dev

MySQL update column value where else update another column value (single query)

From Dev

MySQL query to sum one column and count another column, from two tables, based on a common value?

From Dev

Make a new column of the first n letters of a column, where n is the value in another column

From Dev

MySQL query to get sum where month = January

From Java

R: sum largest n rows into a column

From Dev

Get total value of the same column multiple rows and total of two different rows (mysql query)

From Dev

how to get rows from a table where value of column occurs most in another table?

From Dev

MYSQL: Selecting value in column where column name is defined on a seperate table

From Dev

Split pandas column into multiple rows, where splitting is on the value of another column

From Dev

MySQL query: sum column values with distinct for another column

From Dev

Return rows where sum of a single column is equal to "16" in mysql

From Dev

SELECT SomeColumn where SUM(SomeOtherColumn) of any N rows or less is a certain value

From Dev

Mysql query to get rows of a table as columns of another, with column names from third table

From Dev

MySql get the sum of a column in every rows of a new column

From Dev

mysql select count(column) where sum(column) > value

From Dev

Get number of rows from mysql where condition is met and the max value in 1 query

Related Related

  1. 1

    MySQL Select Query: SUM() rows with a distinct value, from another column

  2. 2

    MySql Query to sum the rows and get the max value

  3. 3

    Get the rows where column/field sum equals a certain value

  4. 4

    MySQL get count of all rows in another table where the value equals column in current table

  5. 5

    mysql query select multiple rows where column value is same

  6. 6

    SQL Query: Get rows where value of certain column is maximum

  7. 7

    How to get the first N rows where the sum is ≥ X?

  8. 8

    How to get the first N rows where the sum is ≥ X?

  9. 9

    MySQL query to get the sum of a column

  10. 10

    Get 'balanced' column value as 'Y' if both rows has same amt and 'N' if not in SQL query

  11. 11

    Get rows where one column is unique and another column is the lowest value relative to the unique column

  12. 12

    Get a table with a value as key and a count or sum of another column, only of rows with that value

  13. 13

    Pandas - New column based on the value of another column N rows back, when N is stored in a column

  14. 14

    MySQL update column value where else update another column value (single query)

  15. 15

    MySQL query to sum one column and count another column, from two tables, based on a common value?

  16. 16

    Make a new column of the first n letters of a column, where n is the value in another column

  17. 17

    MySQL query to get sum where month = January

  18. 18

    R: sum largest n rows into a column

  19. 19

    Get total value of the same column multiple rows and total of two different rows (mysql query)

  20. 20

    how to get rows from a table where value of column occurs most in another table?

  21. 21

    MYSQL: Selecting value in column where column name is defined on a seperate table

  22. 22

    Split pandas column into multiple rows, where splitting is on the value of another column

  23. 23

    MySQL query: sum column values with distinct for another column

  24. 24

    Return rows where sum of a single column is equal to "16" in mysql

  25. 25

    SELECT SomeColumn where SUM(SomeOtherColumn) of any N rows or less is a certain value

  26. 26

    Mysql query to get rows of a table as columns of another, with column names from third table

  27. 27

    MySql get the sum of a column in every rows of a new column

  28. 28

    mysql select count(column) where sum(column) > value

  29. 29

    Get number of rows from mysql where condition is met and the max value in 1 query

HotTag

Archive