宽恕可能无聊的问题。
我正在使用以下示例,即使支出为零,我也想为每个推销员返回一个值。我该如何实现?
SELECT
CAST(coalesce(customer.salesman, '0') AS integer) as Salesman,
Sum(sophead.order_value) AS "Quote Value"
FROM
customer
INNER JOIN sophead ON sophead.inv_account = customer.account
WHERE
customer.company = 2 AND
Extract(MONTH FROM sophead.order_date) = Extract(MONTH FROM Now()) AND
Extract(YEAR FROM sophead.order_date) = Extract(YEAR FROM Now()) AND
sophead.order_type = 'Q' AND
sophead.salesman IN ('21','22','25','28','29','76')
GROUP BY
customer.salesman
ORDER BY
customer.salesman
答案 0 :(得分:0)
您只需要使用LEFT JOIN和COALESCE来确保无销售时显示为0:
SELECT
CAST(coalesce(customer.salesman, '0') AS integer) as Salesman,
COALESCE(Sum(sophead.order_value), 0) AS "Quote Value"
FROM customer
LEFT JOIN sophead
ON sophead.inv_account = customer.account
AND Extract(MONTH FROM sophead.order_date) = Extract(MONTH FROM Now())
AND Extract(YEAR FROM sophead.order_date) = Extract(YEAR FROM Now())
AND sophead.salesman IN ('21','22','25','28','29','76')
WHERE customer.company = 2
AND sophead.order_type = 'Q'
GROUP BY
customer.salesman
ORDER BY
customer.salesman