bodesam bodesam - 2 months ago 8
SQL Question

How to select aggregate of values within a date range from a table

Let me just give a simplified example to illustrate what I mean.

Let's say we have a table named accounts with columns deposits, dmonth, and dyear.
Select sum(deposits) will get the sum of ALL values under the deposits column.
What is the SQL query to get sum of deposits for, say, period earlier than July 2016

Answer

Here is another way:

SELECT SUM(Deposits)
FROM   Accounts
WHERE  (dYear = 2016 AND dMonth < 7)
OR     dYear < 2016