MySQL select records with sum greater than threshold

Michael Babcock

I need to select records based on file size listed in a MySQL database table, with a single query (no store procedures). The record set should contain all records where the sum total file size equals or if needed exceeds a specific threshold. (Example, threshold = 30, results return 3 records with file sizes equal to 10, 10, 20 or 10, 10, 10 or one record with a file size of 32)

table

+----+---------+-----------+
| id | user_id | fileSize  |
+----+---------+-----------+
|  1 |       1 |      9319 |
|  2 |       1 |     51683 |
|  3 |       1 |     19776 |
|  4 |       1 |    395890 |
|  5 |       1 |      7132 |
|  6 |       1 |     97656 |
|  7 |       1 |      9798 |
|  9 |       1 |     16096 |
| 10 |       1 |    113910 |
| 11 |       1 |    160037 |
+----+---------+-----------+

After researching and trying a number of different solutions the best query that I have come up with looks like this:

SELECT f1.user_id, f1.id AS file_id, f1.fileSize, SUM(f2.fileSize) AS totalSum
FROM files AS f1 
INNER JOIN files AS f2 ON f1.id >= f2.id 
WHERE f1.user_id = 1
GROUP BY f1.id 
HAVING totalSum <= 350000;

Example results

+---------+---------+-----------+----------+
| user_id | file_id |  fileSize | totalSum |
+---------+---------+-----------+----------+
|       1 |       1 |      9319 |     9319 |
|       1 |       2 |     51683 |    61002 |
|       1 |       3 |     19776 |    80778 |
+---------+---------+-----------+----------+

Desired results

+---------+---------+-----------+----------+
| user_id | file_id |  fileSize | totalSum |
+---------+---------+-----------+----------+
|       1 |       1 |      9319 |     9319 |
|       1 |       2 |     51683 |    61002 |
|       1 |       3 |     19776 |    80778 |
|       1 |       4 |    395890 |   476668 |
+---------+---------+-----------+----------+

Or

+---------+---------+-----------+----------+
| user_id | file_id |  fileSize | totalSum |
+---------+---------+-----------+----------+
|       1 |       3 |    395890 |   395890 |
+---------+---------+-----------+----------+

What isn't working with the query above is that the threshold will never be met, as it is based on HAVING lesser than the threshold (greater than just returns crazy amounts of records well above the threshold). Also, if there are any records in the set that have a file size exceeding the threshold, the query result sometimes returns empty. Ideal results would meet or slightly exceed the threshold and may contain many records or a single record if the single file size matched or exceeded the threshold.

Any help would be appreciated. I think this is the first time I have posted a question online in about five years. Seriously, been stuck on this for a week. ~ Thx

ROunofF

This seems to be fitted for a UNION resultset. So you have to get 2 queries (one for each "criteria") and join their result using union.

First query would become:

SELECT f1.user_id, f1.id AS file_id, SUM(f1.fileSize) AS totalSum
FROM files AS f1 
WHERE f1.user_id = 1
GROUP BY f1.id 
HAVING totalSum <= 350000;

Now you need the query that select if size is too big:

SELECT f1.user_id, f1.id AS file_id, MAX(f1.fileSize) AS max
FROM files AS f1 
WHERE f1.user_id = 1
GROUP BY f1.id 
HAVING max >= 350000;

Next you want to combine them in a single query. Since both have the same fields you can simply "union" the result

SELECT f1.user_id, f1.id AS file_id, SUM(f1.fileSize) AS totalSum
FROM files AS f1 
WHERE f1.user_id = 1
GROUP BY f1.id 
HAVING totalSum <= 350000
UNION
SELECT f1.user_id, f1.id AS file_id, MAX(f1.fileSize) AS max
FROM files AS f1 
WHERE f1.user_id = 1
GROUP BY f1.id 
HAVING max >= 350000;

PS: You had "ON f1.id >= f2.id" as join criteria, not sure why the > that would be very case-specific :)

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 Records Greater Than OR First

From Dev

SQL select all records only if the sum is greater than 0

From Dev

Select array elements that are greater than 5% of a sum

From Dev

Query where sum of a group of records greater than ZERO

From Dev

Mysql Select datetime, greater than timestamp

From Dev

MYSQL Select Count if greater than 0

From Dev

SQLite select all records for which count is greater than 1

From Dev

Select records having a time interval greater than a certain value in Teradata

From Dev

sum if greater than in r

From Dev

sql select where (sum of 2 columns) is greater than X

From Dev

How to select where sum of fields is greater than a value in MongoDB

From Dev

numpy.argmin for elements greater than a threshold

From Dev

mysql select if count of values for each user greater than 1

From Dev

Select MySQL results, ignoring fields that have a count greater than X

From Dev

MySQL: Select TIMEDIFF values where the difference is greater than x

From Dev

mysql select count for each user if greater than 3

From Dev

MySQL: Select rows with values nearest but greater than a given value

From Dev

MySQL - Select rows greater than timestamp AND specific UUID?

From Dev

'NOT GREATER THAN' condition Mysql

From Dev

'NOT GREATER THAN' condition Mysql

From Dev

Get the sum which is greater than 0 or less than 0 using MySQL

From Dev

Select columns thats greater than?

From Dev

Python - speed up finding percentile of set which is greater than threshold

From Dev

Split list of tuples where second elment is greater than threshold

From Dev

How to extract only values greater than a threshold from a file?

From Dev

Python - speed up finding percentile of set which is greater than threshold

From Dev

Split list of tuples where second elment is greater than threshold

From Dev

SQL Command return user greater than threshold value

From Dev

How to extract only values greater than a threshold from a file?

Related Related

  1. 1

    MYSQL Select Records Greater Than OR First

  2. 2

    SQL select all records only if the sum is greater than 0

  3. 3

    Select array elements that are greater than 5% of a sum

  4. 4

    Query where sum of a group of records greater than ZERO

  5. 5

    Mysql Select datetime, greater than timestamp

  6. 6

    MYSQL Select Count if greater than 0

  7. 7

    SQLite select all records for which count is greater than 1

  8. 8

    Select records having a time interval greater than a certain value in Teradata

  9. 9

    sum if greater than in r

  10. 10

    sql select where (sum of 2 columns) is greater than X

  11. 11

    How to select where sum of fields is greater than a value in MongoDB

  12. 12

    numpy.argmin for elements greater than a threshold

  13. 13

    mysql select if count of values for each user greater than 1

  14. 14

    Select MySQL results, ignoring fields that have a count greater than X

  15. 15

    MySQL: Select TIMEDIFF values where the difference is greater than x

  16. 16

    mysql select count for each user if greater than 3

  17. 17

    MySQL: Select rows with values nearest but greater than a given value

  18. 18

    MySQL - Select rows greater than timestamp AND specific UUID?

  19. 19

    'NOT GREATER THAN' condition Mysql

  20. 20

    'NOT GREATER THAN' condition Mysql

  21. 21

    Get the sum which is greater than 0 or less than 0 using MySQL

  22. 22

    Select columns thats greater than?

  23. 23

    Python - speed up finding percentile of set which is greater than threshold

  24. 24

    Split list of tuples where second elment is greater than threshold

  25. 25

    How to extract only values greater than a threshold from a file?

  26. 26

    Python - speed up finding percentile of set which is greater than threshold

  27. 27

    Split list of tuples where second elment is greater than threshold

  28. 28

    SQL Command return user greater than threshold value

  29. 29

    How to extract only values greater than a threshold from a file?

HotTag

Archive