douqian1296 2019-06-30 16:43
浏览 42
已采纳

如何获得总结为特定值的行的随机组合?

The Table I have:

+--------+--------------------+
|item_id|        value        |
+--------+--------------------+
|  1     | 1                  |
|  2     | 4                  |
|  3     | 2                  |
|  4     | 6                  |
+--------+--------------------+

What the SQL Query Result should be: A random combination of items that sum up to 10 with a variable number of different items (in this case 2.)

+--------------+--------------------+-------------+
|item_id       |        amount      |      sum    |
+--------------+--------------------+-------------+
|2             |2                   |8            |
|3             |1                   |2            |
+--------------+--------------------+-------------+

The results show

You get 2 times item 2 (which has the value of 4 so it adds to a sum of 8).

And you get one time item 3 (which has the value of 2)

This combination sums up to 10.

Is that even possible, when it should not be the same combination always and picked random if there are other possibilitys?

  • 写回答

2条回答 默认 最新

  • dongpingwu8378 2019-06-30 21:58
    关注

    Assuming you want a single random combination, you can do:

    select
      *
    from (
      select
        a.item_id as item1, 
        x.n as amount1, 
        a.value * x.n as sum1,
        b.item_id as item2, 
        y.n as amount2, 
        b.value * y.n as sum2,
        rand() as r
      from my_table a
      join my_table b on b.item_id <> a.item_id
      cross join (
        select 1 as n union select 2 union select 3 union select 4 
        union select 5 union select 6 union select 7 union select 8 
        union select 9 union select 10) x
      cross join (
        select 1 as n union select 2 union select 3 union select 4
        union select 5 union select 6 union select 7 union select 8 
        union select 9 union select 10) y
      where a.value * x.n + b.value * y.n = 10
    ) z
    order by r -- sorted randomly
    limit 1 -- to get only one combination; remove to get them all
    

    Every time you run this query it picks a random [different] solution.

    The script to create the table and data you mentioned (that I used to test) is:

    create table my_table (
      item_id int,
      value int
    );
    
    insert into my_table (item_id, value) values (1, 1);
    insert into my_table (item_id, value) values (2, 4);
    insert into my_table (item_id, value) values (3, 2);
    insert into my_table (item_id, value) values (4, 6);
    

    EDIT on July 1st, 2019: As requested, here's an equivalent [shorter] solution using a recursive CTE (Common Table Expression), available in MariaDB since 10.2.2 (see Recursive Common Table Expressions):

    with recursive
    val as (select 1 as n union all select n + 1 from val where n < 10)
    select
      *
    from (
      select
        a.item_id as item1, 
        x.n as amount1, 
        a.value * x.n as sum1,
        b.item_id as item2, 
        y.n as amount2, 
        b.value * y.n as sum2,
        rand() as r
      from my_table a
      join my_table b on b.item_id <> a.item_id
      cross join val x
      cross join val y
      where a.value * x.n + b.value * y.n = 10
    ) z
    order by r -- sorted randomly
    limit 1 -- to get only one combination; remove to get all 22 answers
    

    This solution scales much better if you need to use higher numbers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?