PHP array_sum 没有添加 sqlsrv 查询
PHP array_sum not adding sqlsrv query
我正在使用 PHP 5.6 和 7.0.29 执行以下 SQLSRV 查询,当我得到结果时,我似乎无法使用 [= 添加(求和)"grand_total" 的总结果32=] 甚至当我尝试循环它时。
$sql = "SELECT inv_trx.trx_date, datepart(day, datediff(day, 0, inv_trx.trx_date)/7 * 7)/7 + 1 AS WEEKNUMBER , DATEPART(WEEK, DATEADD(MM, DATEDIFF(MM,0,'".$datetime."'), 0))+ 1 AS WEEK_OF_MONTH, inv_trx.trx_qty, inv_trx.unit_price, inv_trx.ord_qty, item.prod_cat, inv_trx.slsman_1, SUM(inv_trx.trx_qty * inv_trx.unit_price) AS grand_total
FROM dbo.customer customer, dbo.inv_trx inv_trx, dbo.item item, dbo.ord_hedr ord_hedr
WHERE datepart(day, datediff(day, 0, inv_trx.trx_date)/7 * 7)/7 + 1 = '1' AND customer.cust_no = inv_trx.cust_no AND customer.cust_no = ord_hedr.cust_no AND inv_trx.order_no = ord_hedr.order_no AND inv_trx.item_no = item.item_no AND
inv_trx.manu_no = item.manu_no AND ((ord_hedr.ord_type='O') AND (inv_trx.trx_type='S') AND (ord_hedr.ord_class<>'M' And ord_hedr.ord_class<>'P') AND (customer.exclude_sa=0) OR (ord_hedr.ord_type='C') AND (inv_trx.trx_type='S') AND (ord_hedr.ord_class<>'M' And ord_hedr.ord_class<>'P') AND (customer.exclude_sa=0)) AND YEAR(inv_trx.trx_date) = '2018' AND MONTH(inv_trx.trx_date) = '7' GROUP BY inv_trx.trx_date, DATEPART(WEEK,inv_trx.trx_date), inv_trx.trx_qty, inv_trx.unit_price, inv_trx.ord_qty, item.prod_cat, inv_trx.slsman_1 ";
$params = array();
$result = sqlsrv_query( $conn, $sql );
if( $result === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC) ) {
?> <tr>
<td><?php echo $row['trx_date']->format('m/d/Y')?></td>
<td><?php echo $row['WEEKNUMBER']?></td>
<td><?php echo $row['trx_qty']?></td>
<td><?php echo $row['unit_price']?></td>
<td><?php echo $row['ord_qty']?></td>
<td><?php echo $row['prod_cat']?></td>
<td><?php echo $row['slsman_1']?></td>
<td><?php echo $row['grand_total']?></td>
</tr>
<?php
$group = array($row['grand_total']);
print_r(array_sum($group));
//echo $row['grand_total']; //result is the same
Array_sum 的结果是相同的,只是回应 grand_total 本身。
示例:4.8 6 109.4 1.197 17.8 45.5 89 而不是总计 = 184.XX
这是因为我已经在用 SUM 求和 inv_trx.trx_qty * inv_trx.unit_price 形成 grand_total 了吗?
我怀疑这是我的阵列的问题,但我不确定。任何建议将不胜感激。
查询生成以下内容
Trx Date Week Trx QTY Price Quantity Category Salesman Total
09/02/2018 1 1 4.8 80 MAG Mickey 4.8
09/02/2018 1 3 2 50 BOB Donald 6
09/02/2018 1 4 27.35 4 POW Goose 109.4
我想要做的是得到总和,即 grand_total。
这对于评论来说太长了,并且没有回答您当前遇到的问题,但是您可以使用别名、正确的连接语法和一些类似这样的格式来大大简化您的整个查询。
SELECT it.trx_date
, datepart(day, datediff(day, 0, it.trx_date) / 7 * 7) / 7 + 1 AS WEEKNUMBER
, DATEPART(WEEK, DATEADD(month, DATEDIFF(month, 0, '".$datetime."'), 0)) + 1 AS WEEK_OF_MONTH
, it.trx_qty
, it.unit_price
, it.ord_qty
, i.prod_cat
, it.slsman_1
, SUM(it.trx_qty * it.unit_price) AS grand_total
FROM dbo.customer c
join dbo.inv_trx it on c.cust_no = it.cust_no
join dbo.item i on it.item_no = i.item_no
AND it.manu_no = i.manu_no
join dbo.ord_hedr oh on c.cust_no = oh.cust_no
AND it.order_no = oh.order_no
WHERE datepart(day, datediff(day, 0, it.trx_date) / 7 * 7) / 7 + 1 = 1
AND oh.ord_type in ('O', 'C')
AND it.trx_type = 'S'
AND oh.ord_class not in ('M', 'P')
AND c.exclude_sa = 0
AND YEAR(it.trx_date) = 2018 --date functions return integers not strings
AND MONTH(it.trx_date) = 7
GROUP BY it.trx_date
, DATEPART(WEEK, it.trx_date)
, it.trx_qty
, it.unit_price
, it.ord_qty
, i.prod_cat
, it.slsman_1
--编辑--
添加 GROUPING SETS 示例。
我会看一下 this article 以获得有关其工作原理的很好解释。
SELECT it.trx_date
, datepart(day, datediff(day, 0, it.trx_date) / 7 * 7) / 7 + 1 AS WEEKNUMBER
, DATEPART(WEEK, DATEADD(month, DATEDIFF(month, 0, '".$datetime."'), 0)) + 1 AS WEEK_OF_MONTH
, it.trx_qty
, it.unit_price
, it.ord_qty
, i.prod_cat
, it.slsman_1
, SUM(it.trx_qty * it.unit_price) AS grand_total
FROM dbo.customer c
join dbo.inv_trx it on c.cust_no = it.cust_no
join dbo.item i on it.item_no = i.item_no
AND it.manu_no = i.manu_no
join dbo.ord_hedr oh on c.cust_no = oh.cust_no
AND it.order_no = oh.order_no
WHERE datepart(day, datediff(day, 0, it.trx_date) / 7 * 7) / 7 + 1 = 1
AND oh.ord_type in ('O', 'C')
AND it.trx_type = 'S'
AND oh.ord_class not in ('M', 'P')
AND c.exclude_sa = 0
AND YEAR(it.trx_date) = 2018 --date functions return integers not strings
AND MONTH(it.trx_date) = 7
GROUP BY GROUPING SETS((it.trx_date
, DATEPART(WEEK, it.trx_date)
, it.trx_qty
, it.unit_price
, it.ord_qty
, i.prod_cat
, it.slsman_1), ())
如果你的 T-SQL 语句 returns 行并且你想对结果集中每一行的值求和,你需要在 while () {...}
之前定义你的数组然后添加每行的值:
<?php
...
# Statement
$sql = ' ... your statement ...';
$params = array();
$result = sqlsrv_query($conn, $sql, $params);
if ($result === false) {
die( print_r(sqlsrv_errors(), true));
}
# Results
$total = array();
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
?>
<tr>
<td><?php echo $row['trx_date']->format('m/d/Y')?></td>
<td><?php echo $row['WEEKNUMBER']?></td>
<td><?php echo $row['trx_qty']?></td>
<td><?php echo $row['unit_price']?></td>
<td><?php echo $row['ord_qty']?></td>
<td><?php echo $row['prod_cat']?></td>
<td><?php echo $row['slsman_1']?></td>
<td><?php echo $row['grand_total']?></td>
</tr>
<?php
$total[] = $row['grand_total'];
echo 'Grand total after current row: '.array_sum($total).'<br>';
}
# Total after all rows
echo 'Grand total after all rows: '.array_sum($total).'<br>';
...
?>
我正在使用 PHP 5.6 和 7.0.29 执行以下 SQLSRV 查询,当我得到结果时,我似乎无法使用 [= 添加(求和)"grand_total" 的总结果32=] 甚至当我尝试循环它时。
$sql = "SELECT inv_trx.trx_date, datepart(day, datediff(day, 0, inv_trx.trx_date)/7 * 7)/7 + 1 AS WEEKNUMBER , DATEPART(WEEK, DATEADD(MM, DATEDIFF(MM,0,'".$datetime."'), 0))+ 1 AS WEEK_OF_MONTH, inv_trx.trx_qty, inv_trx.unit_price, inv_trx.ord_qty, item.prod_cat, inv_trx.slsman_1, SUM(inv_trx.trx_qty * inv_trx.unit_price) AS grand_total
FROM dbo.customer customer, dbo.inv_trx inv_trx, dbo.item item, dbo.ord_hedr ord_hedr
WHERE datepart(day, datediff(day, 0, inv_trx.trx_date)/7 * 7)/7 + 1 = '1' AND customer.cust_no = inv_trx.cust_no AND customer.cust_no = ord_hedr.cust_no AND inv_trx.order_no = ord_hedr.order_no AND inv_trx.item_no = item.item_no AND
inv_trx.manu_no = item.manu_no AND ((ord_hedr.ord_type='O') AND (inv_trx.trx_type='S') AND (ord_hedr.ord_class<>'M' And ord_hedr.ord_class<>'P') AND (customer.exclude_sa=0) OR (ord_hedr.ord_type='C') AND (inv_trx.trx_type='S') AND (ord_hedr.ord_class<>'M' And ord_hedr.ord_class<>'P') AND (customer.exclude_sa=0)) AND YEAR(inv_trx.trx_date) = '2018' AND MONTH(inv_trx.trx_date) = '7' GROUP BY inv_trx.trx_date, DATEPART(WEEK,inv_trx.trx_date), inv_trx.trx_qty, inv_trx.unit_price, inv_trx.ord_qty, item.prod_cat, inv_trx.slsman_1 ";
$params = array();
$result = sqlsrv_query( $conn, $sql );
if( $result === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC) ) {
?> <tr>
<td><?php echo $row['trx_date']->format('m/d/Y')?></td>
<td><?php echo $row['WEEKNUMBER']?></td>
<td><?php echo $row['trx_qty']?></td>
<td><?php echo $row['unit_price']?></td>
<td><?php echo $row['ord_qty']?></td>
<td><?php echo $row['prod_cat']?></td>
<td><?php echo $row['slsman_1']?></td>
<td><?php echo $row['grand_total']?></td>
</tr>
<?php
$group = array($row['grand_total']);
print_r(array_sum($group));
//echo $row['grand_total']; //result is the same
Array_sum 的结果是相同的,只是回应 grand_total 本身。
示例:4.8 6 109.4 1.197 17.8 45.5 89 而不是总计 = 184.XX
这是因为我已经在用 SUM 求和 inv_trx.trx_qty * inv_trx.unit_price 形成 grand_total 了吗?
我怀疑这是我的阵列的问题,但我不确定。任何建议将不胜感激。
查询生成以下内容
Trx Date Week Trx QTY Price Quantity Category Salesman Total
09/02/2018 1 1 4.8 80 MAG Mickey 4.8
09/02/2018 1 3 2 50 BOB Donald 6
09/02/2018 1 4 27.35 4 POW Goose 109.4
我想要做的是得到总和,即 grand_total。
这对于评论来说太长了,并且没有回答您当前遇到的问题,但是您可以使用别名、正确的连接语法和一些类似这样的格式来大大简化您的整个查询。
SELECT it.trx_date
, datepart(day, datediff(day, 0, it.trx_date) / 7 * 7) / 7 + 1 AS WEEKNUMBER
, DATEPART(WEEK, DATEADD(month, DATEDIFF(month, 0, '".$datetime."'), 0)) + 1 AS WEEK_OF_MONTH
, it.trx_qty
, it.unit_price
, it.ord_qty
, i.prod_cat
, it.slsman_1
, SUM(it.trx_qty * it.unit_price) AS grand_total
FROM dbo.customer c
join dbo.inv_trx it on c.cust_no = it.cust_no
join dbo.item i on it.item_no = i.item_no
AND it.manu_no = i.manu_no
join dbo.ord_hedr oh on c.cust_no = oh.cust_no
AND it.order_no = oh.order_no
WHERE datepart(day, datediff(day, 0, it.trx_date) / 7 * 7) / 7 + 1 = 1
AND oh.ord_type in ('O', 'C')
AND it.trx_type = 'S'
AND oh.ord_class not in ('M', 'P')
AND c.exclude_sa = 0
AND YEAR(it.trx_date) = 2018 --date functions return integers not strings
AND MONTH(it.trx_date) = 7
GROUP BY it.trx_date
, DATEPART(WEEK, it.trx_date)
, it.trx_qty
, it.unit_price
, it.ord_qty
, i.prod_cat
, it.slsman_1
--编辑--
添加 GROUPING SETS 示例。
我会看一下 this article 以获得有关其工作原理的很好解释。
SELECT it.trx_date
, datepart(day, datediff(day, 0, it.trx_date) / 7 * 7) / 7 + 1 AS WEEKNUMBER
, DATEPART(WEEK, DATEADD(month, DATEDIFF(month, 0, '".$datetime."'), 0)) + 1 AS WEEK_OF_MONTH
, it.trx_qty
, it.unit_price
, it.ord_qty
, i.prod_cat
, it.slsman_1
, SUM(it.trx_qty * it.unit_price) AS grand_total
FROM dbo.customer c
join dbo.inv_trx it on c.cust_no = it.cust_no
join dbo.item i on it.item_no = i.item_no
AND it.manu_no = i.manu_no
join dbo.ord_hedr oh on c.cust_no = oh.cust_no
AND it.order_no = oh.order_no
WHERE datepart(day, datediff(day, 0, it.trx_date) / 7 * 7) / 7 + 1 = 1
AND oh.ord_type in ('O', 'C')
AND it.trx_type = 'S'
AND oh.ord_class not in ('M', 'P')
AND c.exclude_sa = 0
AND YEAR(it.trx_date) = 2018 --date functions return integers not strings
AND MONTH(it.trx_date) = 7
GROUP BY GROUPING SETS((it.trx_date
, DATEPART(WEEK, it.trx_date)
, it.trx_qty
, it.unit_price
, it.ord_qty
, i.prod_cat
, it.slsman_1), ())
如果你的 T-SQL 语句 returns 行并且你想对结果集中每一行的值求和,你需要在 while () {...}
之前定义你的数组然后添加每行的值:
<?php
...
# Statement
$sql = ' ... your statement ...';
$params = array();
$result = sqlsrv_query($conn, $sql, $params);
if ($result === false) {
die( print_r(sqlsrv_errors(), true));
}
# Results
$total = array();
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
?>
<tr>
<td><?php echo $row['trx_date']->format('m/d/Y')?></td>
<td><?php echo $row['WEEKNUMBER']?></td>
<td><?php echo $row['trx_qty']?></td>
<td><?php echo $row['unit_price']?></td>
<td><?php echo $row['ord_qty']?></td>
<td><?php echo $row['prod_cat']?></td>
<td><?php echo $row['slsman_1']?></td>
<td><?php echo $row['grand_total']?></td>
</tr>
<?php
$total[] = $row['grand_total'];
echo 'Grand total after current row: '.array_sum($total).'<br>';
}
# Total after all rows
echo 'Grand total after all rows: '.array_sum($total).'<br>';
...
?>