根据平均值分配行

时间:2019-09-25 12:04:37

标签: php mysql

我正在从csv导入。这些案件需要根据案件的价值平均分配给4个人。值的范围从$ 4到$ 40 000,因此按正常计数的案例数量然后根据该数量进行分配将不起作用。

一个例子,第一个案例可以是2万美元,而其他11个案例可以在200到450美元之间,这将不构成20000美元。因此,第一个用户将获得2万美元,然后其余的收入应平均分配给其余3个用户。

我对如何在mysql中或从php中进行选择然后写回mysql感到困惑。

我已经尝试了平均金额,求和并用4.对行进行编号,然后对其进行排序,并将每4rth分配给一个用户,等等。但是我完全不了解这一点。

SELECT amount as a,'2725.5' as b
FROM `testing_assign`
ORDER BY amount DESC

#where 2725.5 is the average

这就是回报。

"a"     "b"        

"3791"  "2725.5"

"2000"  "2725.5"

"900"   "2725.5"

"890"   "2725.5"

"771"   "2725.5"

"762"   "2725.5"

"620"   "2725.5"

"456"   "2725.5"

"450"   "2725.5"

"189"   "2725.5"

"49"    "2725.5"

"24"    "2725.5"

预期为

a           c

3791        - user 1

2000        - user 2

900         - user 3

890         - user 4

771     - user 3

762         - user 4

620         - user 3

456         - user 4

450         - user 3

189         - user 4

49          - user 2

24          - user 2

user 4 = A4+A6+A8+A10
user 3 = A3+A5+A7+A9
user 2 = A2+A11+A12

,用户1得到a1,因为那是最大,剩下的那个人的平均总和不等于该数量。

类似于更新案例集用户-用户2,金额= 24等

2 个答案:

答案 0 :(得分:0)

我认为当您通过使用求和和分组从数据库本身获取值时,您可以求和每个用户的数量

SELECT sum(amount) as a,'2725.5' as b,`user` as c FROM `testing_assign` GROUP by `user` ORDER BY amount DESC

此SQL查询将返回每个用户数量的总和,就像下面您想要的

 a       b       c
3791  2725.5  user 1 
2073  2725.5  user 2 
2741  2725.5  user 3 
2297  2725.5  user 4 

然后您可以简单地使用您的条件来满足您的需求。

答案 1 :(得分:0)

经过挖掘和搜索以及反复试验后会出现焊接。这是我上面的查询的解决方案。

    <?php
arrayBuilder();
function arrayBuilder()
{
    $list_of_items = [11, 41, 31, 15, 15, 66, 67, 34, 20, 42, 22, 25, 50,43, 22];
    rsort($list_of_items);
    $number_of_cont = 4;
    $weight_of_items = array_sum($list_of_items);
    $weight_per_cont = ($weight_of_items/$number_of_cont);
    $containers = [];
    $sumArray = $newArray = [];
    $containersSorted = buildArray($list_of_items,$number_of_cont,$containers,$weight_per_cont);
    $itemsNotAdded = itemsNotINArray($list_of_items, $containersSorted);
    foreach ($containersSorted as $k => $subArray) {
        foreach ($subArray as $id => $value) {
            if(isset($sumArray[$k])) {
                $sumArray[$k] += $value;
            }else{
                $sumArray[$k] = $value;
            }
        }
    }
    sort($sumArray);
    $itemsNotAdded = array_values($itemsNotAdded);
    foreach($itemsNotAdded as $key => $value){
        foreach($sumArray as $minkey => $minval){
            if($minkey == $key) {
                $containersSorted[$key][] = $value;
                unset($itemsNotAdded[$key]);
            }
        }
    }

    print_r($containersSorted);
};

function itemsNotINArray($list_of_items, $containersSorted){
    foreach($containersSorted as $conid => $conval){
        foreach($list_of_items as $key => $value){
            if(key_exists($key,$conval)){
               unset($list_of_items[$key]);
               continue;
            }
        }
    }
    return $list_of_items;
};

function getMisMatchKey($itemsNotAdded, $containersSorted){

}

function buildArray($list_of_items,$number_of_cont,$containers, $weight_per_cont){
    foreach($list_of_items as $key => $item) {
        for ($i = 0; $i < $number_of_cont; $i++) {
            $total = (isset($containers[$i])) ? array_sum($containers[$i]) : 0;
            if (($total + $item) < $weight_per_cont) {
                $containers[$i][$key] = $item;
                break;
            }
        }
    }
    return $containers;
};