2016-05-31 66 views
0

我希望有人能幫我解決此問題。根據日期範圍查找並計算員工

我在這需要以下結果查詢工作,

  • 辦公室,
  • 子程序,
  • 服務類型,
  • 總#的客戶,其預約日期落在給定的範圍之間 約會狀態爲「已完成」,
  • 開始日期在約會5日內的客戶總數 約會

我寫了下面的代碼來獲取Office,子程序,服務類型和總#的客戶,其預約日期指定日期範圍內,其預約狀態爲「已完成」

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 

並且寫了下面的代碼獲取辦公室,子程序,服務類型和總#客戶的開始日期內預約日期爲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) 

回答

0
SELECT Distinct 
     fa.new_sitename AS Office, 
     fs.new_subprogramname As Subprogram, 
     Fsrv.name AS Servicetype, 
    Count(fa.new_clientidname) As Total#OfClients, 
    count(fa1.new_clientidname) as "Total # of Clients receiving FV within 5 days" 
FROM FirstServiceAppointment fs left outer join Firstappointment fa 
on fs.new_appointmentlookup = fa.new_appointmentid 
left outer join Firstappointment fa1 
on fs.new_appointmentlookup = fa1.new_appointmentid 
AND DATEDIFF(DAY,cast(fa.createdon AS Date),cast(fs.scheduledstart AS Date)) >=5 
join FirstService Fsrv 
on fa.new_service=Fsrv.serviceid or fa1.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 fa1.createdon BETWEEN '05/18/2016' AND '05/30/2016' 
group by fa.new_sitename, fs.new_subprogramname,Fsrv.name