按年和季度MySQL排序后添加百分比

时间:2016-02-25 03:33:58

标签: mysql

以下查询返回年份和季度的订单总计。我还希望将价值列为总支出的百分比。

SELECT DATABASE() AS PURCHASING_Total_PO_Value_By_Year, 
YEAR(order_headers.created_at) AS Year, 
QUARTER(order_headers.created_at) AS Quarter, 
COUNT(DISTINCT(order_headers.id)) AS Orders, 
SUM(order_lines.accounting_total) AS Order_Total 
FROM (order_lines LEFT JOIN order_headers ON order_lines.order_header_id = order_headers.id) 
WHERE (order_headers.status LIKE 'Issued') OR (order_headers.status LIKE 'Closed') 
Group by YEAR(order_headers.created_at), QUARTER(order_headers.created_at)

我需要在不应用年份和季度过滤器的情况下使用SUM(order_lines.accounting_total) as 'TOTAL SPEND'

任何指针/提示都将不胜感激。

Current Query Returns

1 个答案:

答案 0 :(得分:2)

如果您真的只想要N # Append next line to pattern space # If the hostname is missing, insert a newline and a tab s/^([[:space:]]*hardware.*)(\n})$/\1\n\t\2/ t # If we did the substitution, jump to end (prints complete pattern space) P # Print first line in pattern space D # Delete first line in pattern space - starts new cycle 的总计没有年份或季度聚合,那么您可以尝试使用子查询:

Order_Total

我添加到SELECT DATABASE() AS PURCHASING_Total_PO_Value_By_Year, YEAR(order_headers.created_at) AS Year, QUARTER(order_headers.created_at) AS Quarter, COUNT(DISTINCT(order_headers.id)) AS Orders, SUM(order_lines.accounting_total) AS Order_Total, 100 * SUM(order_lines.accounting_total) / (SELECT SUM(accounting_total) FROM order_lists) AS Order_Percentage FROM order_lines LEFT JOIN order_headers ON order_lines.order_header_id = order_headers.id WHERE (order_headers.status LIKE 'Issued') OR (order_headers.status LIKE 'Closed') GROUP BY YEAR(order_headers.created_at), QUARTER(order_headers.created_at) 的专栏是:

SELECT