;with cte_stat as
ALTER PROCEDURE [dbo].[qryAppointmentworkStat]
@startFrom smalldatetime,
@endTo smalldatetime,
@Type int
as
BEGIN
declare @surefromdate smalldatetime
if @startFrom is null
set @surefromdate=cast(convert(varchar(10),getdate(),120) as datetime)
else
set @surefromdate=@startFrom
;with cte_stat as
(
select app.createdBy ,COUNT(*) AS work,app.appType
from appointments app
where app.createdBy is not null
and app.appType='电话预约'
and (appDate between @surefromdate and @endTo)
--and (userType!=0 or userType=@Type)
group by app.createdBy,app.appType
)
select u.userName
,u.realName
,cte_stat.appType
,cte_stat.work
from cte_stat
left join users u on u.userName=cte_stat.createdBy
where u.isBackend=1
END