根据日期范围查找和计算员工

时间:2016-05-31 21:27:19

标签: sql tsql reporting-services

我希望有人可以帮我解决这个问题。

我正在处理一个需要跟随的查询,

  • 办公室,
  • 子程序,
  • 的ServiceType,
  • 预约日期在给定范围之间的客户总数 日期和约会状态为“完成”,
  • 开始日期在约会后5天内的客户总数 日期

我编写了以下代码来获取Office,SubProgram,ServiceType和Total#Of客户的约会日期在给定的日期范围内且其约会状态为“完成”

SELECT
     fa.new_sitename AS Office
    ,fs.new_subprogramname AS Subprogram
    ,Fsrv.NAME AS Servicetype
    ,COUNT(fa.new_clientidname) AS Total#OfClients
FROM 
    Firstappointment fa
INNER JOIN 
    FirstServiceAppointment fs
ON 
    fa.new_appointment id = fs.new_appointment lookup
INNER JOIN 
    FirstService Fsrv
ON 
    fa.new_service = Fsrv.serviceid
WHERE
    fs.new_visittypename LIKE 'First'
    AND fs.statuscodename LIKE 'done'
    AND fa.createdon BETWEEN '05/18/2016' AND '05/30/2016'
GROUP BY
     fa.new_sitename
    ,fs.new_subprogramname
    ,Fsrv.NAME

并编写以下代码以获取Office,SubProgram,ServiceType和客户的总数#,其开始日期在约会日期的5天内

SELECT 
     COUNT(fa.new_clientidname) AS "Total # of Clients receiving FV within 5 days"
    ,fa.new_sitename AS Office
    ,fs.new_subprogramname AS Subprogram
    ,Fsrv.NAME AS Servicetype
FROM 
    Firstappointment fa
INNER JOIN 
    FirstServiceAppointment fs
ON 
    fa.new_appointmentid = fs.new_appointmentlookup
INNER JOIN 
    FirstService Fsrv
ON 
    fa.new_service = Fsrv.serviceid
WHERE 
    fs.new_visittypename LIKE 'first'
    AND fs.statuscodename LIKE 'done'
    AND fa.createdon BETWEEN '05/18/2016' AND '05/30/2016'
    AND DATEDIFF(DAY, cast(fa.createdon AS DATE), cast(fs.scheduledstart AS DATE)) <= 5
GROUP BY 
     fa.new_sitename
    ,fs.new_subprogramname
    ,Fsrv.NAME

现在,如何将这两个结果集合并为一个并使其类似于

Office, Subprogram, ServiceType, Total#OfClients (From First Query), Total # of Clients receiving FV within 5 days" (From Second Query)

1 个答案:

答案 0 :(得分:0)

{{1}}