sql选择sum all rows作为total

sg3maiej  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(382)

我跑了一辆 SELECT 语句来查询这些结果。我想知道如何 SUM “售出数量”的总和。怎么做到的?
结果:

目前,这是一个查询:

SELECT
     "CustomerName",
     "Completed Date",
     "System Size" AS 'Quantity Sold',
     (NOW() -INTERVAL 26 DAY) AS 'Pay Period Start Date',
     (NOW() -INTERVAL 13 DAY) AS 'Pay Period End Date',
     (SELECT SUM("System Size") FROM  Deals) AS 'System Size Installed'
FROM  Deals

然后,我有第二个excel报告,需要手动输入一个“总成本”在上述付款期间。有没有办法让这一步自动化?

qvsjd97n

qvsjd97n1#

SELECT
    "CustomerName",
    DATE("Completed Date")   AS "Completed Date",
    SUM("System Size")       AS "Quantity Sold",
    (NOW() -INTERVAL 26 DAY) AS "Pay Period Start Date",
    (NOW() -INTERVAL 13 DAY) AS "Pay Period End Date",
    (SELECT SUM("System Size") FROM  Deals) AS "System Size Installed"
FROM
    Deals
GROUP BY
    "CustomerName",
    DATE("Completed Date")
ORDER BY
    "CustomerName",
    DATE("Completed Date")

使用 DATE() 只是“以防万一”您的列还包含值的时间部分,但假定它不是字符串列。
另外,我强烈建议不要在列名中使用空格。您不会在变量名中使用空格,所以不要在sql列名中使用空格。。。

相关问题