sql >> Database teknologi >  >> RDS >> Mysql

Træk kolonner fra afledt tabel og summer dem i én MySQL SELECT-sætning

Jeg havde en stavefejl og et formateringsproblem. Ved at formatere de endelige data i stedet for at formatere i den indlejrede SELECT-sætning, var mine tabeldata nøjagtige.

Vellykket 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";

Tak!!!




  1. PostgreSQL, kompleks forespørgsel til beregning af ingredienser efter opskrift

  2. Hvordan finder man den største tabel i MySQL-databasen?

  3. DROP TABEL HVIS FINDER Eksempel i PostgreSQL

  4. To autoincrements-kolonner eller autoincrement og samme værdi i anden kolonne