我有一个关于SQL以及如何按年和月汇总特定行的问题
问题:我想对2016年1月的S& P值(本月内的所有日期+年份)求和 表格如下所示
S&PTable
user_id date price
1 1/2/2016 1989.00 (sum this)
2 1/3/2016 1987.25 (sum this)
3 1/4/2016 1985.50 (sum this)
4 2/1/2016 2011.25 (don't sum this)
5 3/1/2016 2015.75 (don't sum this)
这是我有多远
SELECT SUM(price)
FROM S&PTable
WHERE ?????
答案 0 :(得分:2)
在大多数数据库中,你会写:
SELECT SUM(price)
FROM S&PTable
WHERE date >= '2016-01-01' and date < '2016-02-01';
大多数数据库都不接受表名中的&
,因此需要对其进行转义。
答案 1 :(得分:0)
您也可以使用between
来获得结果
SELECT SUM(price)
FROM S&PTable
WHERE date between '2016-01-01' and '2016-01-31';