我在数据库(MS Access)(水果和蔬菜)中有2个表。它们中的每一个都有“Date”和“Value”字段。可以有几行具有相同的日期。 如何输出一个表格,该表格将显示每行“日期”,“水果价值总和”和“Vgetables的价值总和”?并且,例如,如果某些“日期”的“蔬菜”没有“值”,那么空单元格会出现吗?
答案 0 :(得分:1)
尝试这样的事情(未经测试):
SELECT t.date, SUM(f_val) as f_val, SUM(v_val) AS v_val
FROM (
SELECT date, value AS f_val, 0 AS v_val FROM Fruits
UNION ALL
SELECT date, 0, value FROM Vegetables
) t
GROUP BY t.date
答案 1 :(得分:1)
SELECT Date,Sum(fValue) as FruitsValue,Sum(vValue) as VegtablesValue
FROM
(
SELECT Date,value as fValue,null as vValue FROM Fruits
UNION ALL SELECT Date,null as fValue,Value as vValue FROM Vegetables
) T
GROUP BY Date