Time spent with clients are recorded in minutes. I would like Crystal to select each rep and run a separate report for each of them.
Sql Code:
drop table #upper
select top (5)
LAST_NAME
,FIRST_NAME
,msa.CLIENT_ID
,sum(Minutes) As MinutesInvested
,msa.STAFF_ID
,case when msa.STAFF_ID = '5556918' then 'Arron Smith'
when msa.STAFF_ID = '5550166' then 'Kristyn Smith'
when msa.STAFF_ID = '5521086' then 'Lauren Smith'
when msa.STAFF_ID= '5554256' then 'John Smith'
when msa.STAFF_ID= '5556908' then 'Darianna Smith'
when msa.STAFF_ID = '5559009' then 'William Smith'
when msa.STAFF_ID = '5555499' then 'Samuel Smith'
when msa.STAFF_ID = '5557' then 'Nick Smith'
when msa.STAFF_ID = '5553105' then 'Ryan Smith'
end as Staff_Member
into #upper
from tableOne msa
left join tableTwo dim on msa.CLIENT_ID = dim.CLIENT_ID
--where msa.STAFF_ID in ('5556918','5550166','5521086','5556908','5554256','5559009','5555499','5557','5553105')
where msa.STAFF_ID = '5550166'
AND (SERVICE_DATE > '2015-09-01') AND (SERVICE_DATE < '2015-09-30')
group by msa.CLIENT_ID,STAFF_ID,LAST_NAME,FIRST_NAME
order by MinutesInvested DESC
drop table #lower
SELECT top (5)
LAST_NAME
,FIRST_NAME
,msb.CLIENT_ID
,sum(Minutes) As MinutesInvested
,msb.STAFF_ID
,case when msb.STAFF_ID = '5556918' then 'Arron Smith'
when msb.STAFF_ID = '5550166' then 'Kristyn Smith'
when msb.STAFF_ID = '5521086' then 'Lauren Smith'
when msb.STAFF_ID= '5554256' then 'John Smith'
when msb.STAFF_ID= '5556908' then 'Darianna Smith'
when msb.STAFF_ID = '5559009' then 'William Smith'
when msb.STAFF_ID = '5555499' then 'Samuel Smith'
when msb.STAFF_ID = '5557' then 'Nick Smith'
when msb.STAFF_ID = '5553105' then 'Ryan Smith'
end as Staff_Member
into #lower
from tableOne msb
left join tableTwo dim on msb.CLIENT_ID = dim.CLIENT_ID
--where msb.STAFF_ID in ('5556918','5550166','5521086','5556908','5554256','5559009','5555499','5557','5553105')
where msb.STAFF_ID = '5550166'
AND (SERVICE_DATE > '2015-09-01') AND (SERVICE_DATE < '2015-09-30')
group by msb.CLIENT_ID,STAFF_ID,LAST_NAME,FIRST_NAME
order by MinutesInvested ASC
select distinct
msa.LAST_NAME
,msa.FIRST_NAME
,msa.CLIENT_ID
,msa.MinutesInvested
,msa.STAFF_ID
,msa.Staff_Member
from #upper msa
Union all
select
msb.LAST_NAME
,msb.FIRST_NAME
,msb.CLIENT_ID
,msb.MinutesInvested
,msb.STAFF_ID
,msb.Staff_Member
from #lower msb
Sql Output:
LAST_NAME FIRST_NAME CLIENT_ID MinutesInvested STAFF_ID Staff_Member
CUTHB Smith 5559637 350 5550166 Kristyn Smith
SHARP Smith 5555652 230 5550166 Kristyn Smith
STRAHAN Smith 5555128 420 5550166 Kristyn Smith
SWILLEY Smith 5552734 330 5550166 Kristyn Smith
WOLFE Smith 5558029 200 5550166 Kristyn Smith
MARTINE Smith 5554275 5 5550166 Kristyn Smith
MORGAN Smith 5554307 5 5550166 Kristyn Smith
BENNETT Smith 5559611 5 5550166 Kristyn Smith
BENELL Smith 5554745 5 5550166 Kristyn Smith
PLUMMER Smith 5558420 5 5550166 Kristyn Smith
|