Query Construction: Count, Case, Datediff, and Group By
[Reframing prior question, which had been posed as a question about Cursors.]
I am looking for a way to select counts under certain date conditions.
Say there is a table, T1, with 2 fields (ID, Date). The ID is not a unique
key. The table records events by id, and some ids occur frequently, some
infrequently.
For example:
ID | Date
1 | 2010-01-01
2 | 2010-02-01
3 | 2010-02-15
2 | 2010-02-15
4 | 2010-03-01
I would like to create a new table with the following fields: ID, Date,
Count of times ID appears in 6 months previous to Date, Count of times ID
appears in 6 months after Date.
In essence, for every row in the existing table, I want to add a column
that looks back for times the same ID has appeared in previous six months,
and look ahead for times the same ID has appeared in following six months.
Is there a best way to formulate the appropriate query?
No comments:
Post a Comment