簡體   English   中英

返回數組中相同 id 的多個值

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

我正在從 MSSQL 2008 中的 3 個不同表中提取信息,我想獲得CC_qty的 SUM 以及每個Location壓縮為每個id一個字段。 如果這可以在查詢本身中完成,那就太棒了 - listaggGROUP_CONCAT不會削減它。 否則我一直在使用 array_reduce、array_merge、array_diff 都無濟於事。

這是我的查詢和原始數組:

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
        )

)

這是我想要的輸出,為了簡單地知道我絕對需要哪些信息,我已經刪除了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
            )

    )

感謝您的關注!

由於我為 MySQL 留下了答案,因此它不會為此起作用。 我不太了解 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
        )

)
*/

我對 MSSQL 沒有任何經驗,但我非常有信心它提供了必要的合並、求和和連接功能。 無論如何,我不得不發布一個答案,因為我發現 Thomas 的答案不夠完善。

本質上,您應該使用id值作為臨時鍵來確定您是在處理組的第一次出現還是后續出現。 在第一次遇到時,只需將整行保存到輸出數組。 對於屬於同一組的所有未來行,只需求和並連接所需的值。

要刪除結果數組中的臨時鍵,只需調用array_values($result)

代碼:(演示

$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));

輸出:

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