Ok so I have a table with, amongst other, columns: name, c_id, date.
Certain entries in that table obey a certain criteria, which I can effectively select using WHERE, so this is not a problem.
What I would like, then, is a way to group by, in addition to the name and c_id columns, a group of three dates: date - 1, date, and date + 1. In other words, I want each row of the output to represent all entries that have the same name and c_id as a certain relevant entry and which happened between the day before and the day after that entry, including itself.
How would I go about doing that?
--EDIT:
(EDIT2: The origin table is supposed to be an INNER JOIN of Table1 and Table2 ON Table1.id = Table2.id)
Sample data:
Table1:
id | c_id | date | other stuff
-----------------------------------------------------
01 | abc | 2015/12/09 | whatever
02 | abc | 2015/12/09 | whatever
03 | abc | 2015/12/10 | relevant criterion
04 | abc | 2015/12/11 | whatever
05 | def | 2015/11/15 | whatever
06 | def | 2015/11/16 | relevant criterion
07 | abc | 2015/11/17 | whatever
08 | mnc | 2016/01/02 | whatever
09 | mnc | 2016/01/02 | whatever
10 | mnc | 2016/01/03 | whatever
11 | mnc | 2016/01/03 | whatever
12 | mnc | 2016/01/03 | whatever
13 | mnc | 2016/01/04 | relevant criterion
14 | mnc | 2016/01/05 | whatever
15 | mnc | 2016/01/05 | whatever
16 | mnc | 2016/01/06 | whatever
Table2:
id | Name | other stuff
--------------------------------------
01 | John | whatever
02 | John | whatever
03 | John | whatever
04 | John | whatever
05 | Mary | whatever
06 | Mary | whatever
07 | Mary | whatever
08 | Alice | whatever
09 | Alice | whatever
10 | Alice | whatever
11 | Alice | whatever
12 | Alice | whatever
13 | Alice | whatever
14 | Alice | whatever
15 | Alice | whatever
16 | Alice | whatever
Sample desired output:
Name | c_id | pivot_date | count
------------------------------------------
John | abc | 2015/12/10 | 4
Mary | def | 2015/11/16 | 2
Alice | mnc | 2016/01/04 | 6
(The pivot_date part is not particularly necessarily the one with the relevant criterion, any one of the dates involved are good.)