use HistoricalData
select
CONVERT(DATE,[HistoricalData].[dbo].[GetRealDate] ('PSTMX',StartDate)) as StartDatePST
--,a.Direction
,(CASE WHEN a.Direction='S' AND a.OutboundProcessId ='' THEN 'OutBound_Manual'
WHEN a.Direction='S' THEN 'OutBound_Engine'
WHEN a.Direction='E' THEN 'InBound'
ELSE NULL END) as [Type]
,a.Campaign
,a.LastAgent
--,a.AttentionTime
,(CASE WHEN a.AttentionTime<60 THEN '[<60]'
WHEN a.AttentionTime between 60 and 120 THEN '[60-120]'
WHEN a.AttentionTime between 121 and 240 THEN '[121-240]'
WHEN a.AttentionTime between 241 and 300 THEN '[241-300]'
WHEN a.AttentionTime>300 THEN '[>300]'
ELSE NULL END) as AttTimeRange
,COUNT(*) as Calls
from HistoricalData.dbo.InteractionDetail a
where
StartDate >= [HistoricalData].[dbo].[GetInvRealDate] ('PSTMX','2018-01-01') -- <-- Start date****************
AND StartDate < [HistoricalData].[dbo].[GetInvRealDate] ('PSTMX','2018-01-31') -- <-- Today's date*******************
and a.IsTaked=1
group by
CONVERT(DATE,[HistoricalData].[dbo].[GetRealDate] ('PSTMX',StartDate))
--,a.Direction
,(CASE WHEN a.Direction='S' AND a.OutboundProcessId ='' THEN 'OutBound_Manual'
WHEN a.Direction='S' THEN 'OutBound_Engine'
WHEN a.Direction='E' THEN 'InBound'
ELSE NULL END)
,a.Campaign
,a.LastAgent
--,a.AttentionTime
,(CASE WHEN a.AttentionTime<60 THEN '[<60]'
WHEN a.AttentionTime between 60 and 120 THEN '[60-120]'
WHEN a.AttentionTime between 121 and 240 THEN '[121-240]'
WHEN a.AttentionTime between 241 and 300 THEN '[241-300]'
WHEN a.AttentionTime>300 THEN '[>300]'
ELSE NULL END)
order by 1