Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql查询以获取小于由另一个字段分组的限制的字段的总和_Mysql_Sql - Fatal编程技术网

mysql查询以获取小于由另一个字段分组的限制的字段的总和

mysql查询以获取小于由另一个字段分组的限制的字段的总和,mysql,sql,Mysql,Sql,我有一张桌子,上面写着: order_id int ,type string ,weight double. 例如: | 4 | type1 | 9.729999542236328 | | 5 | type2 | 13.930000305175781 | | 14 | type4 | 9.399999618530273 | | 17 | type1 | 3.490000009536743 | | 20 | type

我有一张桌子,上面写着:

order_id int ,type string ,weight double.
例如:

|        4 |  type1 |  9.729999542236328 |
|        5 |  type2 | 13.930000305175781 |
|       14 |  type4 |  9.399999618530273 |
|       17 |  type1 |  3.490000009536743 |
|       20 |  type3 |  6.349999904632568 |
|       25 |  type3 | 12.869999885559082 |
|       31 |  type4 | 1.3700000047683716 |
|       40 |  type5 | 20.079999923706055 |
|       42 |  type2 |    9.0600004196167 |
|       45 |  type2 | 15.390000343322754 |
{order_ids: [1, 2, 3], total_weight: 450 }
{order_ids: [4, 5, 6], total_weight: 470 }
{order_ids: [7, 8, 9], total_weight: 400 }
我想按id对总重量小于500的行进行分组

例如:

|        4 |  type1 |  9.729999542236328 |
|        5 |  type2 | 13.930000305175781 |
|       14 |  type4 |  9.399999618530273 |
|       17 |  type1 |  3.490000009536743 |
|       20 |  type3 |  6.349999904632568 |
|       25 |  type3 | 12.869999885559082 |
|       31 |  type4 | 1.3700000047683716 |
|       40 |  type5 | 20.079999923706055 |
|       42 |  type2 |    9.0600004196167 |
|       45 |  type2 | 15.390000343322754 |
{order_ids: [1, 2, 3], total_weight: 450 }
{order_ids: [4, 5, 6], total_weight: 470 }
{order_ids: [7, 8, 9], total_weight: 400 }
我想得到订单的ID和总重量。我有200000多条线在桌子上,所以性能对我来说是一个很大的焦点。我没有分享任何查询,因为我不知道从哪里开始。 我将golang与gorm和mysql 8.0.21一起使用。 我不需要找到最优的解决方案,它可以是FIFO。

选择GROUP\u CONCAT(订单id按订单id排序)order\u id,
SELECT GROUP_CONCAT(order_id ORDER BY order_id) order_ids, 
       SUM(weight) total_weight
FROM (SELECT test.*,
             @current_group := @current_group + (@current_weight + weight > @max_weight) group_number,
             @current_weight := weight + @current_weight * (@current_weight + weight <= @max_weight) cumulative_weight
      FROM test, (SELECT @current_weight := 0, @current_group := 0) variables
      ORDER BY order_id) subquery
GROUP BY group_number;
总和(重量)总重量 从(选择测试。*), @当前组:=@当前组+(@当前组重量+重量>@最大组重量)组数量,
@当前权重:=权重+@当前权重*(@当前权重+权重您可以使用递归CTE:

with tt as (
      select tt.*,
             row_number() over (order by rand()) as seqnum
      from t
     ),
     recursive cte (
      select order_id, weight, weight as running_weight, 1 as grp
      from tt
      where seqnum = 1
      union all
      select tt.order_id, tt.weight,
             (case when tt.weight + cte.running_weight >= 500 
                   then tt.weight else tt.weight + cte.running_weight
              end),
             (case when tt.weight + cte.running_weight >= 500
                   then grp + 1 else grp
              end)
      from cte join
           tt
           on tt.seqnum = cte.seqnum + 1
     )
select *
from cte;

请将示例数据替换为与所需示例输出严格匹配的数据。我将测试哪一个具有更好的性能谢谢!@fbarril…这将很有趣。返回报告。我希望变量可能会稍好一些。也就是说,在
select
语句中设置变量是不推荐的在8.0版中使用arting。