I have the following sql:
Which gives me these results:
Description Date Vol Client Sum
------------
Call back scheduled 24/03/2014 2 SWWAGED 4516.04
Data Protection Act Refusal - RPC 24/03/2014 1 SWWAGED 2083.13
Data Protection Act Refusal - RPC 21/03/2014 1 SWWAGED 2083.13
What I need is:
Description Date Vol Client Sum
------------
Call back scheduled 24/03/2014 2 SWWAGED 4516.04
Data Protection Act Refusal - RPC 24/03/2014 1 SWWAGED 2083.13
I need to retrieve the most recent record (by date) and group.
Essentially from my data I need to group by description and group by date.
Then I need to get only the latest records by group with the most recent dates, count them and sum the values
Code:
select c.FullCodeDescription,
l.[Date],
COUNT(l.Curr1) as Volume,
d.clientcode,
sum(l.Curr1) as value
from openquery([bcw-apps],'select bcwref,clientcode,code from dial_attempts_js where clientcode in (''SWWAGED'',''SWWAGEDSME'') and code in (26,59,63) and jobdate between cast(floor( cast( dateadd(day,-7,getDate()) AS float) )AS datetime) and cast(floor( cast( dateadd(day,-1,getDate()) AS float) )AS datetime) ') d
inner join BusinessSupportProfiling..CompletionCodes c ON d.code = c.Code
inner join BusinessSupportProfiling..DailyOutboundCallingList l ON d.bcwref COLLATE DATABASE_DEFAULT = l.BCWRef COLLATE DATABASE_DEFAULT
where CONVERT(datetime,substring(l.[Date],7,4) + substring(l.[Date],4,2) + substring(l.[Date],1,2)) between cast(floor( cast( dateadd(day,-7,getDate()) AS float) )AS datetime) and cast(floor( cast( dateadd(day,-1,getDate()) AS float) )AS datetime)
group by c.FullCodeDescription,l.[date],d.clientcode
ORDER BY Date Desc
Which gives me these results:
Description Date Vol Client Sum
------------
Call back scheduled 24/03/2014 2 SWWAGED 4516.04
Data Protection Act Refusal - RPC 24/03/2014 1 SWWAGED 2083.13
Data Protection Act Refusal - RPC 21/03/2014 1 SWWAGED 2083.13
What I need is:
Description Date Vol Client Sum
------------
Call back scheduled 24/03/2014 2 SWWAGED 4516.04
Data Protection Act Refusal - RPC 24/03/2014 1 SWWAGED 2083.13
I need to retrieve the most recent record (by date) and group.
Essentially from my data I need to group by description and group by date.
Then I need to get only the latest records by group with the most recent dates, count them and sum the values