I hove some data

ID  |Price | Date 
01  | 100  | 2019-04-11
02  |200  | 2019-04-12
03  |300  | 2019-05-10

How can I get the SUM of Price from April's MONTH (4) only?

2 Answers

0
nikhil sugandh On

this will work:

select sum(a.Price),month(a.Date)
from Table1 a
where month(a.Date)=4
group by month(a.Date);

check :http://sqlfiddle.com/#!9/cfe06/4

you can also make functional based index on month(a.Date) for faster processing as GordonLinoff mentioned!!!

0
Gordon Linoff On

I strongly recommend writing the logic like this, assuming that you mean April of 2019 rather than all Aprils over time:

select sum(t.price)
from t
where t.date >= '2019-04-01' and
      t.date < '2019-05-01';

When you use a function like month() on a column, that almost always prevents the use of indexes on the column. Direct comparisons are fine, though.