我正在尝试重构我的数组,以便我可以在一个最终数组中获得所需的输出,该数组将用于填充csv,最终填充电子表格。
我的第一个msyql查询和结果数组给了我这个结构:
array(
[0] => Array
(
[sku_id] => 1
[dealer_id] => 1976
[locations] => 1
[groupID] => 1
[frame] => 1051
[cover] => 1150
[color] => 99
[start_date] => 2018-03-
)
[1] => Array
(
[sku_id] => 1
[dealer_id] => 5400
[locations] => 1
[groupID] => 1
[frame] => 1051
[cover] => 1150
[color] => 99
[start_date] => 2017-04-
)
[2] => Array
(
[sku_id] => 1
[dealer_id] => 11316
[locations] => 1
[groupID] => 1
[frame] => 1051
[cover] => 1150
[color] => 99
[start_date] => 2017-02-
)
)
所以我得到3条记录,每条记录用于同一产品(sku_id),但是不同的客户(dealer_id)。然后我在DB2中的类似表中匹配这些数据,以获取sku_id的每个订单的数量。第二个查询/数组显示了这个:
Array
(
[0] => Array
(
[CSTNOC] => 1976
[TOTALQTY] => 2
)
[1] => Array
(
[CSTNOC] => 5400
[TOTALQTY] => 5
)
[2] => Array
(
[CSTNOC] => 11316
[TOTALQTY] => 14
)
)
但是,这正是我想要实现的目标。
在下面的示例中,sku_id,groupID和location将直接来自第一个查询。天数是start_date(来自查询1)和curDate()之间的天数。 Qty是来自查询2的orqtyc,而Average是基于位置,天和数量的计算。每行都基于不同的客户,因此即使下面的示例是针对一个sku,行也表示具有不同位置,数量等的不同客户。
SKU_id | groupID | locations (n) | Days (x) | Qty(q) | Average (x/(q/n))
--------------------------------------------------------------------------------
123 1 3 120 15 24
123 1 2 12 6 4
报告或CSV只会显示以上内容的一个订单项:
SKU | Group | Average Days | Total units sold
123 | 1 | 28 | 21
基本上,对于每个sku我都会获得这些记录,然后我需要按行进行这些计算,并获得每个sku的总计。该报告每个sku只有一个订单项。
我拥有实现目标所需的一切,但如何构建最终数组以获得上述所需的输出?
全文:
$skuQuery = "
SELECT
sku_id,
dealer_id,
locations,
s.sku_group_id as groupID,
s.frame as frame,
s.cover1 as cover,
s.color1 as color,
start_date - interval 7 day as start_date
from products p
inner join skus s on p.sku_id = s.id
where curdate() between p.start_date and p.expire_date
group by sku_id, dealer_id
limit 3";
$skuRslt = mysqli_query($conn,$skuQuery);
while($skuRow = mysqli_fetch_assoc($skuRslt)){
$skuResult[] = $skuRow;
$dealerQuery = "
SELECT
cstnoc,
sum(orqtyc) as TotalQTY
from table
where cstnoc = {$skuRow['dealer_id']}
AND framec = {$skuRow['frame']}
AND colr1c = {$skuRow['color']}
AND covr1c = {$skuRow['cover']}
AND extd2d >= " . str_replace('-', '', $skuRow['start_date']) . "
group by cstnoc, framec
";
$dealerRslt = odbc_exec($DB2Conn, $dealerQuery);
foreach($skuResult as $skuRow){
while($dealerRow = odbc_fetch_array($dealerRslt)){
$dealerResult[] = $dealerRow;
}
}
print_r($dealerResult);
}