我目前正在使用表格列运行一些查询:
unit_cost_cents, quantity_submitted, quantity_accepted
我想获得COST百分比和单位百分比。
SELECT
SUM(CAST(unit_cost_cents * quantity_submitted / 100.00 AS MONEY)) AS submitted,
SUM(CAST(unit_cost_cents * quantity_accepted / 100.00 AS MONEY)) AS accepted,
accepted / submitted AS percent_dollars_accepted,
quantity_submitted,
quantity_accepted,
quantity_submitted / quantity_accepted AS percent_units_accepted
结果不是预期的结果。
答案 0 :(得分:0)
回答我自己的问题,继续我的回答我很满意:)
必须除以每个的总和:
SELECT
(DATE_TRUNC('month', (delivery_window_start::timestamptz - INTERVAL '0 second') AT TIME ZONE 'Etc/UTC') + INTERVAL '0 second') AT TIME ZONE 'Etc/UTC' AS month,
SUM(CAST(unit_cost_cents * quantity_submitted / 100.00 AS MONEY)) AS "Submitted",
SUM(CAST(unit_cost_cents * quantity_accepted / 100.00 AS MONEY)) AS "Accepted",
SUM(CAST(unit_cost_cents * (quantity_submitted - quantity_accepted) / 100.00 AS MONEY)) AS "Not Accepted",
(SUM(unit_cost_cents * quantity_accepted)::float / SUM(unit_cost_cents * quantity_submitted)::float * 100)::numeric(7,2) AS "Confirmation Rate",
(SUM(unit_cost_cents * (quantity_submitted - quantity_accepted))::float / SUM(unit_cost_cents * quantity_submitted)::float * 100)::numeric(7,2) AS "Cancellation Rate",
SUM(quantity_submitted) as "Quantity Submitted",
SUM(quantity_accepted) as "Quantity Accepted",
SUM(quantity_submitted - quantity_accepted) as "Quantity Not Accepted",
(SUM(quantity_accepted)::decimal / SUM(quantity_submitted)::decimal * 100)::numeric(7,2) as "Confirmation Rate(Units)",
(SUM(quantity_submitted - quantity_accepted)::decimal / SUM(quantity_submitted)::decimal * 100)::numeric(7,2) as "Cancellation Rate(Units)"
FROM account_purchase_orders
WHERE account_purchase_orders.account_id = #{account_id}
GROUP BY month
ORDER BY month ASC