I see this pattern all the time: you need aggregated data from one table, but from multiple different ranges. A very common example of this is a table that holds some kind of date-related data, and a report that displays current month and year to date. For the purposes of this article, let's create a simple, denormalized table that holds accounting data:
create table Account (
AccountNum int
)
create table AccountActivity (
AccountNum int,
Date datetime,
Amount money
)
To generate the kind of results I described above, I often see queries that look something like this:
create procedure...