Good morning, I'm trying to get the campaign name with the max time so I'm only showing one record per CampaignName. Max doesn't seem to be working probably because the filter name is different.
Can anyone help me with this? I suspect I will need to do a subquery of some sort
SELECT a.CampaignName, b.Filtername, MAX(calldisconnectedtimeUTC) as MaxTime
FROM I3_IC.ININ_DIALER_40.CallHistory a
Inner Join I3_IC.ININ_DIALER_40.CampaignFilterDefinition b On a.CampaignFilterID = b.CampaignFilterID
Where CampaignName like 'GBR%' and datediff(day,calldisconnectedtimeUTC, getdate()) = 0 and a.CampaignFilterID > 0
GROUP BY CampaignName, b.Filtername
Order By CampaignName
Thanks, I'm almost there but I've noticed that for the campaign name GBR Collections it's returning me the filter 'GBR - Defaults' instead of 'GBR - Priority Collections' but I'm unsure why because the MaxTime is greater for the 'GBR - Priority Collections' filter.
Have I gone wrong somewhere?
With CTE as
(
SELECT
a.CampaignName, b.Filtername,
row_number() over (Partition By a.CampaignName order by MAX(calldisconnectedtimeUTC)) as RowNumber,
Sum(Case When a.campaignfilterid > 0 Then 1 Else 0 End) as Dials,
MAX(calldisconnectedtimeUTC) as MaxTime
FROM I3_IC.ININ_DIALER_40.CallHistory a
Inner Join I3_IC.ININ_DIALER_40.CampaignFilterDefinition b On a.CampaignFilterID = b.CampaignFilterID
Where CampaignName like 'GBR%' and datediff(day,calldisconnectedtimeUTC, getdate()) = 0 and a.CampaignFilterID > 0
GROUP BY CampaignName, b.Filtername)
--Order By CampaignName)
Select * from cte where RowNumber = 1
;
WITH cte AS
(
SELECT a.campaignname,
b.filtername,
Sum(
CASE
WHEN a.campaignfilterid > 0 THEN 1
ELSE 0
END) AS dials,
Max(calldisconnectedtimeutc) AS maxtime
FROM i3_ic.inin_dialer_40.callhistory a
INNER JOIN i3_ic.inin_dialer_40.campaignfilterdefinition b
ON a.campaignfilterid = b.campaignfilterid
WHERE campaignname LIKE 'GBR%'
AND Datediff(day, calldisconnectedtimeutc, Getdate()) = 0
AND a.campaignfilterid > 0
GROUP BY campaignname,
b.filtername )
, cte_rownum AS
(
SELECT Row_number() OVER ( partition BY a.campaignname ORDER BY maxtime DESC ) as rownumber, * FROM cte
)
SELECT *
FROM cte_rownum
WHERE rownumber = 1
Msg 4104, Level 16, State 1, Line 21
The multi-part identifier "a.campaignname" could not be bound.
WITH cte AS
(
SELECT a.campaignname,
b.filtername,
Sum(
CASE
WHEN a.campaignfilterid > 0 THEN 1
ELSE 0
END) AS dials,
Max(calldisconnectedtimeutc) AS maxtime
FROM i3_ic.inin_dialer_40.callhistory a
INNER JOIN i3_ic.inin_dialer_40.campaignfilterdefinition b
ON a.campaignfilterid = b.campaignfilterid
WHERE campaignname LIKE 'GBR%'
AND Datediff(day, calldisconnectedtimeutc, Getdate()) = 0
AND a.campaignfilterid > 0
GROUP BY campaignname,
b.filtername )
, cte_rownum AS
(
SELECT Row_number() OVER ( partition BY a.campaignname ORDER BY maxtime DESC ) as rownumber, * FROM cte
)
SELECT *
FROM cte_rownum
WHERE rownumber = 1
WITH cte AS
(
SELECT a.campaignname,
b.filtername,
Sum(
CASE
WHEN a.campaignfilterid > 0 THEN 1
ELSE 0
END) AS dials,
Max(calldisconnectedtimeutc) AS maxtime
FROM i3_ic.inin_dialer_40.callhistory a
INNER JOIN i3_ic.inin_dialer_40.campaignfilterdefinition b
ON a.campaignfilterid = b.campaignfilterid
WHERE campaignname LIKE 'GBR%'
AND Datediff(day, calldisconnectedtimeutc, Getdate()) = 0
AND a.campaignfilterid > 0
GROUP BY campaignname,
b.filtername )
, cte_rownum AS
(
SELECT Row_number() OVER ( partition BY campaignname ORDER BY maxtime DESC ) as rownumber, * FROM cte
)
SELECT *
FROM cte_rownum
WHERE rownumber = 1