我对一个查询有疑问。
SELECT
CASE WHEN b.value IS NULL
THEN sum(d.value)
ELSE sum(d.value) + sum(b.value)
END AS sumaWydatkow
FROM data d
LEFT JOIN data_bills b
ON d.id = b.data_id
WHERE d.type = 0
UNION
SELECT
CASE WHEN b.value IS NULL
THEN sum(d.value)
ELSE sum(d.value) + sum(b.value)
END AS sumaPrzychodow
FROM data d
LEFT JOIN data_bills b
ON d.id = b.data_id
WHERE d.type = 1
我想对两列求和,或者如果其中一列(data_bills)为空,则只对一个表求和,列类型有两个版本,0 和 1。
但现在是一个 clumn 有两个记录。我想要两列,每列一列。我在问是否可以通过一个查询来完成?
谢谢