SELECT unit.*, COALESCE(sum(detail.order_money), 0) as order_amount, COALESCE(sum(detail.one_commission), 0) as one_commission, COALESCE(sum(detail.two_commission), 0) as two_commission, COALESCE((sum(detail.two_commission) + sum(detail.one_commission)), 0) as total_commission FROM profit_dividend_unit unit LEFT OUTER JOIN profit_dividend_detail detail ON unit.dividend_code = detail.dividend_code GROUP BY unit.unit_id 这个不使用GROUP BY,怎么写?
时间: 2024-03-01 09:49:19 浏览: 128
dw中SUM函数的使用.zip_SUM_dw_sum dw_sum函数
如果不使用 GROUP BY 子句,则可以使用子查询来实现相同的结果。例如:
SELECT unit.*,
(SELECT COALESCE(SUM(order_money), 0) FROM profit_dividend_detail WHERE dividend_code = unit.dividend_code) as order_amount,
(SELECT COALESCE(SUM(one_commission), 0) FROM profit_dividend_detail WHERE dividend_code = unit.dividend_code) as one_commission,
(SELECT COALESCE(SUM(two_commission), 0) FROM profit_dividend_detail WHERE dividend_code = unit.dividend_code) as two_commission,
(SELECT COALESCE(SUM(one_commission), 0) + COALESCE(SUM(two_commission), 0) FROM profit_dividend_detail WHERE dividend_code = unit.dividend_code) as total_commission
FROM profit_dividend_unit unit;
该语句将返回与原始查询相同的结果,但是使用了子查询来替代 GROUP BY 子句。
阅读全文