I am trying to retrieve the count of records between a certain date range that is specific to the users min date and max date in another table.
This is what I have so far, however it is excluding at least 13 records that I know of. Can you tell if there is an error in my logic?
Thanks in advance for any input you have!
SELECT rtam.dbo.wfm_process_instance.user_id AS user_id,
MIN(rtam.dbo.wfm_process_instance.LOCAL_USER_START_TIME) AS Min_Date,
MAX(rtam.dbo.wfm_process_instance.LOCAL_USER_START_TIME) AS Max_Date,
0 AS IVR_Calls,
COUNT(*) AS Total_Calls
FROM rtam.dbo.WFM_PROCESS_INSTANCE
LEFT OUTER JOIN
rtam.dbo.WFM_PROCESS_type
ON rtam.dbo.wfm_process_instance.PROCESS_TYPE_INDX = rtam.dbo.wfm_process_type.INDX
WHERE rtam.dbo.wfm_process_type.DISPLAY_NAME = 'DTV Inbound2'
AND EXISTS (SELECT rtam.dbo.gnr_Tbl_72_type.CTRL_USER_ID,
CONVERT (VARCHAR (10), MIN(rtam.dbo.gnr_tbl_72_type.LOCAL_COL_113), 101) AS min_date,
CONVERT (VARCHAR (10), MAX(rtam.dbo.gnr_tbl_72_type.local_col_113), 101) AS max_date
FROM rtam.dbo.GNR_TBL_72_TYPE
WHERE rtam.dbo.GNR_TBL_72_TYPE.CTRL_USER_ID = rtam.dbo.wfm_process_instance.USER_ID
GROUP BY rtam.dbo.GNR_TBL_72_TYPE.CTRL_USER_ID
HAVING rtam.dbo.wfm_process_instance.LOCAL_USER_START_TIME BETWEEN CONVERT (VARCHAR (10), MIN(rtam.dbo.gnr_tbl_72_type.LOCAL_COL_113), 101) AND CONVERT (VARCHAR (10), MAX(rtam.dbo.gnr_tbl_72_type.LOCAL_COL_113), 101))
GROUP BY rtam.dbo.wfm_process_instance.USER_ID
ORDER BY rtam.dbo.wfm_process_instance.USER_ID;
Try:
You might also think about using aliases so that you don't have to repeat lengthy and error-prone references like
rtam.dbo.wfm_process_instance
all over your code.