Pull columns from derived table and sum them in one MySQL SELECT statement

百般思念 提交于 2019-12-31 05:21:07

问题


I need to pull column data from two tables, run calculations on the data with the result saved as an alias, and then sum those results into other alias' to display in a php table. I am trying to achieve this by creating a derived table within my SELECT statement but it doesn't work. I don't receive any errors but my table only displays the column headers.

CODE:

$sql = "SELECT x.company, x.stagestatus, x.shippeddate, SUM(x.totprice) as totalprice, SUM(x.sgtotquantity) as sgtotqty, SUM(x.sgtotalsqft) as sgtotsqft, SUM(x.avgsqftrev) as avgsqftrevenue, SUM(x.avgunitrev) as avgunitrevenue FROM (SELECT t1.company, t1.stagestatus, t1.shippeddate, t1.id, FORMAT(TRIM(LEADING '$' FROM t1.totalprice), 2) AS totprice, t2.invoiceid, SUM(t2.quantity) AS sgtotqauntity, FORMAT(SUM(t2.width * t2.height * t2.quantity ) /144, 2) AS sgtotalsqft, FORMAT((TRIM(LEADING '$' FROM t1.totalprice)/(SUM(t2.width * t2.height * t2.quantity ) /144)), 2) as avgsqftrev, FORMAT((TRIM(LEADING '$' FROM t1.totalprice) / SUM(t2.quantity)), 2) AS avgunitrev
  FROM invoices AS t1 INNER JOIN lineitems AS t2 ON t1.id = t2.invoiceid
  WHERE (t2.invoiceid = t1.id)
  GROUP BY t1.id) x
WHERE x.stagestatus='Complete'
GROUP BY x.company ASC";

This code breaks but when I use the smaller pieces individually, it works ok.

EX:

$sql="SELECT invoices.id, invoices.orderdate, invoices.stagestatus, FORMAT(TRIM(LEADING '$' FROM invoices.totalprice), 2) AS totalprice, clients.company, lineitems.invoiceid, SUM(lineitems.quantity) AS sgtotqty, FORMAT(SUM(lineitems.width * lineitems.height * lineitems.quantity ) /144, 2) AS sgtotsqft, FORMAT((TRIM(LEADING '$' FROM invoices.totalprice)/(SUM(lineitems.width * lineitems.height * lineitems.quantity ) /144)), 2) as avgsqftrevenue, FORMAT((TRIM(LEADING '$' FROM invoices.totalprice) / SUM(lineitems.quantity)), 2) AS avgunitrevenue
FROM clients
INNER JOIN invoices ON clients.id = invoices.clientid
INNER JOIN lineitems ON invoices.id = lineitems.invoiceid
WHERE (lineitems.invoiceid = invoices.id) AND invoices.orderdate BETWEEN '".$revenuefrom."' AND '".$revenueto."' AND invoices.stagestatus IN (". implode(',', array_map(function($item) {return '"' . $item . '"'; }, $revenue_check)) .")
GROUP BY invoices.id DESC";

This code works just fine and groups all data by invoices.id. However, the project needs were adjusted and now everything must group by invoices.company. When I simply try to group by invoices.company instead of invoices.id, my table completes but the values for each company row are very inaccurate, (aren't sum()ing right).

PHP CODE where table is built:

$result = $conn->query($sql);


    echo "<table id='revenueReportA' align='center' class='report_DT'>
    <thead>
    <tr>

    <th>Customer</th>
    <th>Total Revenue</th>
    <th>Total SQ FT</th>
    <th>AVG Revenue Per SQ FT</th>
    <th>Total Number of Units</th>
    <th>AVG Revenue Per Unit</th>
    </tr>
    </head>";


 if ($result = $conn->query($sql)) {

   // fetch associative array 
  while ($row = $result->fetch_assoc()) {

  echo "<tbody>";
  echo "<tr>";
  echo "<td>" . $row['company'] . "</td>";
  echo "<td>" ."$". $row['totalprice'] . "</td>";
  echo "<td>" . $row['sgtotsqft'] ."&nbsp;&nbsp;". "ft<sup>2</sup>". "</td>";
  echo "<td>" ."$". $row['avgsqftrevenue'] . "</td>";
  echo "<td>" . $row['sgtotqty'] . "</td>";
  echo "<td>" ."$". $row['avgunitrevenue'] . "</td>";
  echo "</tr>";
  echo "</tbody>";
  } 

   echo "</table>";

echo "<BR>";

All help is appreciated.

Thank you,


回答1:


I had a spelling mistake and a formatting issue. By formatting the final data instead of formatting within the embedded SELECT statement, my table data was accurate.

Successful CODE:

$sql = "SELECT x.company, x.stagestatus, x.shippeddate, FORMAT(SUM(x.totprice), 2) as totalprice, FORMAT(SUM(x.sgtotquantity), 2) as sgtotqty, FORMAT(SUM(x.sgtotalsqft), 2) as sgtotsqft, FORMAT(SUM(x.avgsqftrev), 2) as avgsqftrevenue, FORMAT(SUM(x.avgunitrev), 2) as avgunitrevenue FROM (SELECT t1.company, t1.stagestatus, t1.shippeddate, t1.id, TRIM(LEADING '$' FROM t1.totalprice) AS totprice, t2.invoiceid, SUM(t2.quantity) AS sgtotquantity, SUM(t2.width * t2.height * t2.quantity ) /144 AS sgtotalsqft, (TRIM(LEADING '$' FROM t1.totalprice)/(SUM(t2.width * t2.height * t2.quantity ) /144)) as avgsqftrev, (TRIM(LEADING '$' FROM t1.totalprice) / SUM(t2.quantity)) AS avgunitrev
FROM invoices AS t1 INNER JOIN lineitems AS t2 ON t1.id = t2.invoiceid
WHERE (t2.invoiceid = t1.id)
GROUP BY t1.id) x
WHERE x.stagestatus='Complete'
GROUP BY x.company ASC";

Thank you!!!



来源:https://stackoverflow.com/questions/40266767/pull-columns-from-derived-table-and-sum-them-in-one-mysql-select-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!