bodesam bodesam - 1 year ago 62
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 Source

Here is another way:

SELECT SUM(Deposits)
FROM   Accounts
WHERE  (dYear = 2016 AND dMonth < 7)
OR     dYear < 2016
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download