简体   繁体   English

返回数组中相同 id 的多个值

[英]Returning Multiple Values of the same id in array

I'm pulling information from 3 different tables in MSSQL 2008 and I'd like to get the SUM of CC_qty as well as each Location condensed into one field per id .我正在从 MSSQL 2008 中的 3 个不同表中提取信息,我想获得CC_qty的 SUM 以及每个Location压缩为每个id一个字段。 If this can be done in the query itself that would be fantastic - listagg and GROUP_CONCAT are not cutting it.如果这可以在查询本身中完成,那就太棒了 - listaggGROUP_CONCAT不会削减它。 Otherwise I've been working with array_reduce, array_merge, array_diff to no avail.否则我一直在使用 array_reduce、array_merge、array_diff 都无济于事。

Here is my query and the original array:这是我的查询和原始数组:

SELECT a.id, a.qty, b.locationID, b.CC_qty, c.Location FROM (
  SELECT left(id, 10) as id, MAX(qty) as qty
  FROM db1
  WHERE id like 'abc-abc%'
  GROUP BY left(id, 10)
) as a
JOIN (
  SELECT locationID, left(SKU, 10) as SKU, CC_qty FROM db2
  WHERE CC_qty > 25
) as b on a.abc-abc = b.SKU
JOIN (
  SELECT locationID, Location FROM db3
) as c on b.locationID = c.locationID


Array
(
    [0] => Array
        (
            [id] => abc-abc-12
            [qty] => 0
            [locationID] => 276
            [CC_qty] => 250
            [Location] => NOP11
        )

    [1] => Array
        (
            [id] => abc-abc-12
            [qty] => 0
            [locationID] => 310
            [CC_qty] => 1385
            [Location] => NOP01
        )

    [2] => Array
        (
            [id] => abc-abc-23
            [qty] => 0
            [locationID] => 84
            [CC_qty] => 116
            [Location] => NOP06
        )

    [3] => Array
        (
            [id] => abc-abc-23
            [qty] => 0
            [locationID] => 254
            [CC_qty] => 432
            [Location] => NOP08
        )

    [4] => Array
        (
            [id] => abc-abc-23
            [qty] => 0
            [locationID] => 228
            [CC_qty] => 101
            [Location] => NOP04
        )

    [5] => Array
        (
            [id] => abc-abc-34
            [qty] => 0
            [locationID] => 254
            [CC_qty] => 436
            [Location] => NOP08
        )

    [6] => Array
        (
            [id] => abc-abc-34
            [qty] => 0
            [locationID] => 254
            [CC_qty] => 62
            [Location] => NOP08
        )

    [7] => Array
        (
            [id] => abc-abc-45
            [qty] => 0
            [locationID] => 75
            [CC_qty] => 89
            [Location] => NOP05
        )

    [8] => Array
        (
            [id] => abc-abc-45
            [qty] => 0
            [locationID] => 202
            [CC_qty] => 372
            [Location] => NOP07
        )

)

This is my desired output, for simplicity of knowing what information I absolutely require I've removed qty and locationID but those don't have to be removed:这是我想要的输出,为了简单地知道我绝对需要哪些信息,我已经删除了qtylocationID但那些不必删除:

 Array
    (
        [0] => Array
            (
                [id] => abc-abc-12
                [CC_qty] => 1635
                [Location] => NOP11, NOP01
            )

        [1] => Array
            (
                [id] => abc-abc-23
                [CC_qty] => 649
                [Location] => NOP06, NOP08, NOP04
            )

        [2] => Array
            (
                [id] => abc-abc-34
                [CC_qty] => 495
                [Location] => NOP08

        [3] => Array
            (
                [id] => abc-abc-45
                [CC_qty] => 461
                [Location] => NOP05, NOP07
            )

    )

Thanks for looking!感谢您的关注!

Being that I left an answer for MySQL, it wasn't going to work for this.由于我为 MySQL 留下了答案,因此它不会为此起作用。 I don't know MSSQL well enough to use it, so here's a way to do it with PHP so I don't leave you completely without an answer.我不太了解 MSSQL 无法使用它,所以这里有一种用 PHP 来完成它的方法,所以我不会让你完全没有答案。

$arr = array
(
    array
    (
        'id' => 'abc-abc-12',
        'qty' => 0,
        'locationID' => 276,
        'CC_qty' => 250,
        'Location' => 'NOP11'
    ),
    array
    (
        'id' => 'abc-abc-12',
        'qty' => 0,
        'locationID' => 310,
        'CC_qty' => 1385,
        'Location' => 'NOP01'
    ),
    array
    (
        'id' => 'abc-abc-23',
        'qty' => 0,
        'locationID' => 84,
        'CC_qty' => 116,
        'Location' =>  'NOP06'
    )
);

$combinedArr = array();

foreach ($arr as $a)
{
    $found = false;

    foreach ($combinedArr as $i => $b)
    {
        if ($b['id'] == $a['id'])
        {
            $found = true;
            $locs  = explode(',', $a['Location']);

            $combinedArr[$i]['CC_qty'] += $a['CC_qty'];

            if (!in_array($b['Location'], $locs))
            {
                $locs[] = $b['Location'];
                $combinedArr[$i]['Location'] = implode(', ', $locs);
            }
        }
    }

    if (!$found)
        $combinedArr[] = $a;
}

print_r($combinedArr);

/*
Array
(
    [0] => Array
        (
            [id] => abc-abc-12
            [qty] => 0
            [locationID] => 276
            [CC_qty] => 1635
            [Location] => NOP01, NOP11
        )

    [1] => Array
        (
            [id] => abc-abc-23
            [qty] => 0
            [locationID] => 84
            [CC_qty] => 116
            [Location] => NOP06
        )

)
*/

I don't have any experience with MSSQL, but I feel rather confident that it provides the necessary functionality to merge, sum, and concatenate.我对 MSSQL 没有任何经验,但我非常有信心它提供了必要的合并、求和和连接功能。 Anyhow, I am compelled to post an answer because I find the answer from Thomas to be unrefined.无论如何,我不得不发布一个答案,因为我发现 Thomas 的答案不够完善。

Essentially, you should use the id values as temporary keys to determine if you are processing the first occurrence of the group or a subsequent occurrence.本质上,您应该使用id值作为临时键来确定您是在处理组的第一次出现还是后续出现。 On the first encounter, just save the whole row to the output array.在第一次遇到时,只需将整行保存到输出数组。 For all future rows belonging to the same group, just sum and concatenate the desired values.对于属于同一组的所有未来行,只需求和并连接所需的值。

To remove the temporary keys in the result array, just call array_values($result) .要删除结果数组中的临时键,只需调用array_values($result)

Code: ( Demo )代码:(演示

$array = [
    ['id' => 'abc-abc-12', 'qty' => 0, 'locationID' => 276, 'CC_qty' => 250, 'Location' => 'NOP11'],
    ['id' => 'abc-abc-12', 'qty' => 0, 'locationID' => 310, 'CC_qty' => 1385, 'Location' => 'NOP01'],
    ['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 84, 'CC_qty' => 116, 'Location' => 'NOP06'],
    ['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 432, 'Location' => 'NOP08'],
    ['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 228, 'CC_qty' => 101, 'Location' => 'NOP04'],
    ['id' => 'abc-abc-34', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 436, 'Location' => 'NOP08'],
    ['id' => 'abc-abc-34', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 62, 'Location' => 'NOP08'],
    ['id' => 'abc-abc-45', 'qty' => 0, 'locationID' => 75, 'CC_qty' => 89, 'Location' => 'NOP05'],
    ['id' => 'abc-abc-45', 'qty' => 0, 'locationID' => 202, 'CC_qty' => 372, 'Location' => 'NOP07'],
];

$result = [];
foreach ($array as $row) {
    if (!isset($result[$row['id']])) {
        $result[$row['id']] = $row;
    } else {
        $result[$row['id']]['qty'] += $row['qty'];                      // SUM
        $result[$row['id']]['locationID'] .= ", " . $row['locationID']; // CONCAT
        $result[$row['id']]['CC_qty'] += $row['CC_qty'];                // SUM
        $result[$row['id']]['Location'] .= ", " . $row['Location'];     // CONCAT
    }
}
var_export(array_values($result));

Output:输出:

array (
  0 => 
  array (
    'id' => 'abc-abc-12',
    'qty' => 0,
    'locationID' => '276, 310',
    'CC_qty' => 1635,
    'Location' => 'NOP11, NOP01',
  ),
  1 => 
  array (
    'id' => 'abc-abc-23',
    'qty' => 0,
    'locationID' => '84, 254, 228',
    'CC_qty' => 649,
    'Location' => 'NOP06, NOP08, NOP04',
  ),
  2 => 
  array (
    'id' => 'abc-abc-34',
    'qty' => 0,
    'locationID' => '254, 254',
    'CC_qty' => 498,
    'Location' => 'NOP08, NOP08',
  ),
  3 => 
  array (
    'id' => 'abc-abc-45',
    'qty' => 0,
    'locationID' => '75, 202',
    'CC_qty' => 461,
    'Location' => 'NOP05, NOP07',
  ),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM