如何在一个查询中汇总多个表的函数结果?

时间:2013-11-04 18:11:54

标签: sql-server function group-by

我有两张桌子,例如ONE和TWO,我的桌子更大,结构不同,但有些字段重合。

enter image description here

如何获取表QUERY?

我只使用一个WITH GROUP和SUM表可以做到这一点,但是如何在QUERY中添加最后一列?有可能吗?

SELECT Date, SUM(Apples), SUM(Oranges) FROM One
GROUP BY Date

2 个答案:

答案 0 :(得分:0)

SELECT one.Date, one.place, SUM(Apples), SUM(Oranges), SUM(Grapes) 
FROM One left join Two on one.place = two.place and one.date = two.date
GROUP BY one.date, one.place

将返回第一个表中的所有位置和正确的计数

答案 1 :(得分:0)

SELECT Date, one.place, SUM(Apples), SUM(Oranges), SUM(Grapes) 
FROM One left join Two on one.place = two.place and one.[Date] = two.[Date]
GROUP BY Date