我有一个这样的表:
from_date | fare
2016-09-23 | 10.35
2016-09-23 | 7.50
2016-09-24 | 20.05
2016-09-25 | 9.45
我希望返回票价的不同总和。 返回的结果应该是这样的:
from_date | fare
2016-09-23 | 17.85
2016-09-24 | 20.05
2016-09-25 | 9.45
我尝试使用此查询
SELECT SUM(FARE) as FARE FROM (SELECT DISTINCT from_date_time as datefrom, fare as FARE) t
但它并没有以我想要的方式回归。 我该如何构建我的查询?
答案 0 :(得分:1)
首先,你永远不需要一些不同的值。如果您发现自己需要,那么您的数据模型就会出现严重问题
其次,您的样本数据表明您只需要一笔金额:
select from_date, sum(fare)
from singletable t
group by from_date;
答案 1 :(得分:0)
您只需按日期对数据进行分组(按字段u需要区分)
SELECT from_date, SUM(fare) as fare FROM table_t GROUP BY from_date