skip to Main Content

I have a table like this:

id date capacity depot reserved order_id
1 2024-04-29 69 1 0 0
2 2024-04-29 49 1 0 0
3 2024-04-29 69 1 0 0
4 2024-04-29 69 1 0 0

Now I want to get only row where the sum of "capacity" is nearly to 100 – so my output should be

id date capacity depot reserved order_id
1 2024-04-29 69 1 0 0
2 2024-04-29 49 1 0 0

Could you help me find the correct SQL statement?

I tried already to sum the capacity and group it by id with having sum >= 100 but I still get no results

2

Answers


  1. See the linked fiddle, but I was able to get results for the test data you provided with the following:

    create table test (
    id int,
      date date, 
      capacity int, depot int,
      reserved tinyint(1),
      order_id int);
      
    
    insert into test values (1, '2024-04-29', 69, 1, 0, 0), (2, '2024-04-29', 49, 1 ,0 ,0),
    ( 3, '2024-04-29', 69,1, 0, 0),
    (4 ,'2024-04-29' ,69 , 1, 0 , 0);
    
    Records: 4  Duplicates: 0  Warnings: 0
    

    Here I’m assuming you want to sum by date, but if that’s not the case you should be able to swap out the aggregation column easily:

    select date, sum(capacity) as total from test group by date having total > 100;
    
    date total
    2024-04-29 256

    fiddle

    Login or Signup to reply.
  2. A sample.

    Caution! Do not reproduce on large datasets, this may hang your database server!

    -- Create a table with the sample data - 10 rows with random values.
    
    CREATE TABLE test (
      id INT AUTO_INCREMENT PRIMARY KEY,
      val INT
    )
    WITH RECURSIVE cte (num) AS (
      SELECT 1 
      UNION ALL 
      SELECT num + 1 FROM cte LIMIT 10
    )
    SELECT NULL id, 10 + RAND() * 50 val FROM cte;
    SELECT * FROM test;
    
    id val
    1 18
    2 55
    3 13
    4 39
    5 44
    6 45
    7 32
    8 15
    9 18
    10 35
    -- The task is to find a combination 
    -- where the SUM of `val` values is close but not exceed the limit.
    
    SET @limit = 100;
    

    The solution. In practice we need the closest combination only, so LIMIT 1 should be added to the query. I have not added it, but the rows numbering is added instead. This allows you to understand the amount of investigated combinations and, hence, the size of temporary buffer needed.

    WITH RECURSIVE
    cte1 (id, val, rn) AS (
      SELECT id, val, ROW_NUMBER() OVER (ORDER BY val DESC, id ASC)
      FROM test
      ),
    cte2 (id, val, rn, path, vals, total) AS (
      SELECT id, val, rn,
             CAST(id AS CHAR(65535)),
             CAST(val AS CHAR(65535)),
             val
      FROM cte1
      UNION ALL
      SELECT cte1.id, cte1.val, cte1.rn, 
             CONCAT_WS(',', cte2.path, cte1.id), 
             CONCAT_WS(',', cte2.vals, cte1.val), 
             cte2.total + cte1.val
      FROM cte2
      JOIN cte1 ON cte1.rn > cte2.rn
      WHERE cte2.total + cte1.val <= @limit
      )
    SELECT ROW_NUMBER() OVER (ORDER BY total DESC, path ASC) rn,
           path, vals, total
    FROM cte2 
    ORDER BY total DESC, path ASC /* LIMIT 1 */ ;
    
    rn path vals total
    1 10,7,1,8 35,32,18,15 100
    2 10,7,9,8 35,32,18,15 100
    3 2,6 55,45 100
    4 2,7,3 55,32,13 100
    5 10,1,9,8,3 35,18,18,15,13 99
    6 2,5 55,44 99
    7 4,7,8,3 39,32,15,13 99
    8 6,4,8 45,39,15 99
    9 10,7,1,3 35,32,18,13 98
    10 10,7,9,3 35,32,18,13 98
    11 5,4,8 44,39,15 98
    12 6,10,1 45,35,18 98
    13 6,10,9 45,35,18 98
    14 5,10,1 44,35,18 97
    15 5,10,9 44,35,18 97
    16 6,4,3 45,39,13 97
    17 5,4,3 44,39,13 96
    18 6,1,9,8 45,18,18,15 96
    19 7,1,9,8,3 32,18,18,15,13 96
    20 10,7,8,3 35,32,15,13 95
    21 5,1,9,8 44,18,18,15 95
    22 6,10,8 45,35,15 95
    23 6,7,1 45,32,18 95
    24 6,7,9 45,32,18 95
    25 2,4 55,39 94
    26 5,10,8 44,35,15 94
    27 5,7,1 44,32,18 94
    28 5,7,9 44,32,18 94
    29 6,1,9,3 45,18,18,13 94
    30 5,1,9,3 44,18,18,13 93
    31 6,10,3 45,35,13 93
    32 4,10,1 39,35,18 92
    33 4,10,9 39,35,18 92
    34 5,10,3 44,35,13 92
    35 6,7,8 45,32,15 92
    36 2,1,9 55,18,18 91
    37 5,7,8 44,32,15 91
    38 6,1,8,3 45,18,15,13 91
    39 6,9,8,3 45,18,15,13 91
    40 2,10 55,35 90
    41 4,1,9,8 39,18,18,15 90
    42 5,1,8,3 44,18,15,13 90
    43 5,9,8,3 44,18,15,13 90
    44 6,7,3 45,32,13 90
    45 4,10,8 39,35,15 89
    46 4,7,1 39,32,18 89
    47 4,7,9 39,32,18 89
    48 5,7,3 44,32,13 89
    49 6,5 45,44 89
    50 2,1,8 55,18,15 88
    51 2,9,8 55,18,15 88
    52 4,1,9,3 39,18,18,13 88
    53 2,7 55,32 87
    54 4,10,3 39,35,13 87
    55 10,1,9,8 35,18,18,15 86
    56 2,1,3 55,18,13 86
    57 2,9,3 55,18,13 86
    58 4,7,8 39,32,15 86
    59 10,7,1 35,32,18 85
    60 10,7,9 35,32,18 85
    61 4,1,8,3 39,18,15,13 85
    62 4,9,8,3 39,18,15,13 85
    63 10,1,9,3 35,18,18,13 84
    64 4,7,3 39,32,13 84
    65 6,4 45,39 84
    66 2,8,3 55,15,13 83
    67 5,4 44,39 83
    68 7,1,9,8 32,18,18,15 83
    69 10,7,8 35,32,15 82
    70 10,1,8,3 35,18,15,13 81
    71 10,9,8,3 35,18,15,13 81
    72 6,1,9 45,18,18 81
    73 7,1,9,3 32,18,18,13 81
    74 10,7,3 35,32,13 80
    75 5,1,9 44,18,18 80
    76 6,10 45,35 80
    77 5,10 44,35 79
    78 6,1,8 45,18,15 78
    79 6,9,8 45,18,15 78
    80 7,1,8,3 32,18,15,13 78
    81 7,9,8,3 32,18,15,13 78
    82 5,1,8 44,18,15 77
    83 5,9,8 44,18,15 77
    84 6,7 45,32 77
    85 5,7 44,32 76
    86 6,1,3 45,18,13 76
    87 6,9,3 45,18,13 76
    88 4,1,9 39,18,18 75
    89 5,1,3 44,18,13 75
    90 5,9,3 44,18,13 75
    91 4,10 39,35 74
    92 2,1 55,18 73
    93 2,9 55,18 73
    94 6,8,3 45,15,13 73
    95 4,1,8 39,18,15 72
    96 4,9,8 39,18,15 72
    97 5,8,3 44,15,13 72
    98 10,1,9 35,18,18 71
    99 4,7 39,32 71
    100 2,8 55,15 70
    101 4,1,3 39,18,13 70
    102 4,9,3 39,18,13 70
    103 10,1,8 35,18,15 68
    104 10,9,8 35,18,15 68
    105 2,3 55,13 68
    106 7,1,9 32,18,18 68
    107 10,7 35,32 67
    108 4,8,3 39,15,13 67
    109 10,1,3 35,18,13 66
    110 10,9,3 35,18,13 66
    111 7,1,8 32,18,15 65
    112 7,9,8 32,18,15 65
    113 1,9,8,3 18,18,15,13 64
    114 10,8,3 35,15,13 63
    115 6,1 45,18 63
    116 6,9 45,18 63
    117 7,1,3 32,18,13 63
    118 7,9,3 32,18,13 63
    119 5,1 44,18 62
    120 5,9 44,18 62
    121 6,8 45,15 60
    122 7,8,3 32,15,13 60
    123 5,8 44,15 59
    124 6,3 45,13 58
    125 4,1 39,18 57
    126 4,9 39,18 57
    127 5,3 44,13 57
    128 2 55 55
    129 4,8 39,15 54
    130 10,1 35,18 53
    131 10,9 35,18 53
    132 4,3 39,13 52
    133 1,9,8 18,18,15 51
    134 10,8 35,15 50
    135 7,1 32,18 50
    136 7,9 32,18 50
    137 1,9,3 18,18,13 49
    138 10,3 35,13 48
    139 7,8 32,15 47
    140 1,8,3 18,15,13 46
    141 9,8,3 18,15,13 46
    142 6 45 45
    143 7,3 32,13 45
    144 5 44 44
    145 4 39 39
    146 1,9 18,18 36
    147 10 35 35
    148 1,8 18,15 33
    149 9,8 18,15 33
    150 7 32 32
    151 1,3 18,13 31
    152 9,3 18,13 31
    153 8,3 15,13 28
    154 1 18 18
    155 9 18 18
    156 8 15 15
    157 3 13 13

    fiddle

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search