Here is my simplied code:
SELECT
a.user_id as User_ID,
min(b.a_day) as Date_from,
max(b.a_day) as Date_to,
c.code as ID
FROM a, b, c
WHERE
a_day > (day, -15, getdate())
GROUP BY
a.user_id,
c.code
Query gives the following output:
User ID date_from date_to id
1234567 2016-06-13 2016-06-13 B
1234567 2016-06-17 2016-06-17 A
12345672016-06-18 2016-06-18 A
1234567 2016-06-19 2016-06-19 A
1234567 2016-06-20 2016-06-20 A
1234567 2016-06-21 2016-06-21 C
1234567 2016-06-22 2016-06-22 C
1234567 2016-06-23 2016-06-23 D
I need something like this:
User ID date_from date_to id
1234567 2016-06-13 2016-06-13 B
1234567 2016-06-17 2016-06-20 A
1234567 2016-06-21 2016-06-22 C
1234567 2016-06-23 2016-06-23 D
When I use min() and max() function with group by, it aggregates fine for all records but I have to aggregate only dates with the same ID day after day.
Any ideas?
Thanks in advance.