For every NAME, I want to query data for the CALL_DATE that is less than or equal to the three months from START_DATE. Below is my attempt but I am getting an error.
Query:
SELECT CALLER_NAME AS "NAME",
CAST(CREATED AS DATE) AS "CALL_DATE",
DURATION,
CAST(START_DATE AS DATE) AS "START_DATE"
FROM table1
GROUP BY NAME
HAVING CAST(CREATED AS DATE) >= MIN(CAST(START_DATE AS DATE)) AND CAST(CREATED AS DATE) <= DATEADD(MONTH, 3, CAST(START_DATE AS DATE))
ORDER BY AE_NAME, CALL_DATE
Error
Msg 8121, Level 16, State 1, Line 12 Column 'CREATED' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8121, Level 16, State 1, Line 12 Column 'START_DATE' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
I understand why I am getting this error but I don't know how to fix this. I'll very much appreciate your help.