2013-04-22 51 views
0

我想創建一個報表來提供來自SQL Server數據庫的數據。我有3個表格我感興趣,客戶,推薦,任命。客戶可以有1. *推介,推薦可以有0. *約會預約。AVG(DATEDIFF(d,Tabl1.date1,MIN(Table2.date1)))T-SQL

在我的報告中,我想展示從接受推薦到第一次約會的平均時間。

我在我的存儲過程中嘗試上述,但收到「無法對包含聚合或子查詢的表達式執行聚合函數」。

有沒有一種很好的方式來讓這個工作沒有你能想到的子查詢?

編輯下面的粗糙表結構。

客戶 ClientID的INT的IIdentity(PK) 用名字VARCHAR(50) 姓VARCHAR(50) DOB DATETIME

推薦 ReferralID INT IDENTITY(PK) ClientID的INT(FK) ReferralRequestReceivedDate DATETIME OrgaisationAreaId INT(fk)

約會 AppintmentID INT IDENTITY(PK) ReferralId INT(FK) AppointmentDate DATETIME AttendanceTypeId INT(FK) AppointmentTypeID INT(FK)

AttendanceTypes AttendanceTypeID INT IDENTITY(PK) 名稱VARCHAR(50)

AppointmentTypes AppointmentTypeID INT IDENTITY(PK) 名稱VARCHAR(50)

OrganisationArea OrgaisationAreaId INT IDENTITY(PK) 名稱VARCHAR(50)

我現有的PROC具有計數按年齡和出勤鍵入如下出席約會......

SELECT OA.Name, 
COUNT(CASE WHEN AppointmentTypeId IN(1,3) 
       AND AppointmentDate BETWEEN '27 Jan 2013' AND '13 Apr 2013' 
       THEN AppointmentId END) AS AppsBooked, 
AVG(DATEDIFF(d, ReferralRequestReceivedDate, MIN(A.AppointmentDate))) 
AS AvgAllocationWaitTime 

FROM OrganisationAreas OA 
LEFT OUTER JOIN Clients C 
    ON OA.OrganisationAreaId = C.OrganisationAreaId 
LEFT OUTER JOIN IaptReferrals R 
    ON C.ClientId = R.ClientId 
LEFT OUTER JOIN IaptAppointments A 
    ON R.IaptReferralId = A.Referral_IaptReferralId 

GROUP BY OA.OrganisationAreaId, OA.Name 
+0

是什麼這些表的結構?什麼鍵鏈接在一起? – 2013-04-22 20:26:43

+0

爲什麼你想這個工作沒有子查詢? – 2013-04-23 05:35:26

回答

1

我想不出辦法,而不使用子查詢或CTE .. 但如果你是確定與子查詢,試試這個

SELECT OA.Name, 
COUNT(CASE WHEN 
      AppointmentTypeId IN(1,3) AND AppointmentDate BETWEEN '27 Jan 2013' AND '13 Apr 2013' THEN AppointmentId 
     END) AS AppsBooked, 
AVG(DATEDIFF(d, ReferralRequestReceivedDate, sq.MinAppointmentDate)) AS AvgAllocationWaitTime 
FROM OrganisationAreas OA 
LEFT OUTER JOIN 
(
    SELECT C.OrganisationAreaId, MIN(A.AppointmentDate) MinAppointmentDate 
    FROM IaptAppointments A 
    LEFT OUTER JOIN IaptReferrals R ON R.IaptReferralId = A.Referral_IaptReferralId 
    LEFT OUTER JOIN Clients C ON C.ClientId = R.ClientId 
    GROUP BY C.OrganisationAreaId 
) sq ON sq.OrganisationAreaId = OA.OrganisationAreaId 
GROUP BY OA.OrganisationAreaId, OA.Name 

或CTE版本:

;WITH cte AS 
(
    SELECT C.OrganisationAreaId, MIN(A.AppointmentDate) MinAppointmentDate 
    FROM IaptAppointments A 
    LEFT OUTER JOIN IaptReferrals R ON R.IaptReferralId = A.Referral_IaptReferralId 
    LEFT OUTER JOIN Clients C ON C.ClientId = R.ClientId 
    GROUP BY C.OrganisationAreaId 
) 
SELECT OA.Name, 
COUNT(CASE WHEN 
      AppointmentTypeId IN(1,3) AND AppointmentDate BETWEEN '27 Jan 2013' AND '13 Apr 2013' THEN AppointmentId 
     END) AS AppsBooked, 
AVG(DATEDIFF(d, ReferralRequestReceivedDate, cte.MinAppointmentDate)) AS AvgAllocationWaitTime 
FROM OrganisationAreas OA 
LEFT OUTER JOIN cte ON cte.OrganisationAreaId = OA.OrganisationAreaId 
GROUP BY OA.OrganisationAreaId, OA.Name 
+0

頂級建議Ethan。乾杯。使用CTE版本並且喜歡它。 – CheGuevarasBeret 2013-04-24 15:26:25

0

你有這個:

SELECT OA.Name, 
COUNT(CASE WHEN AppointmentTypeId IN(1,3) 
       AND AppointmentDate BETWEEN '27 Jan 2013' AND '13 Apr 2013' 
       THEN AppointmentId END) AS AppsBooked, 
AVG(DATEDIFF(d, ReferralRequestReceivedDate, MIN(A.AppointmentDate))) 
AS AvgAllocationWaitTime 

FROM OrganisationAreas OA 
LEFT OUTER JOIN Clients C 
    ON OA.OrganisationAreaId = C.OrganisationAreaId 
LEFT OUTER JOIN IaptReferrals R 
    ON C.ClientId = R.ClientId 
LEFT OUTER JOIN IaptAppointments A 
    ON R.IaptReferralId = A.Referral_IaptReferralId 

GROUP BY OA.OrganisationAreaId, OA.Name 

讓我們看看我們如何改進它。首先,如果您希望介紹日期和首次預約之間的平均時間,則需要內部連接,而不是外部連接。其次,你有一個沒有其他條件的情況構造。三,你有什麼關於預訂似乎無關緊要的約會。最後,你得到了錯誤的語法錯誤信息。

要修復最後一個錯誤,請將最小約會日期移至子查詢。這將使您的查詢看起來像這樣:

select oa.name 
, avg(datediff(d, ReferralRequestReceivedDate, MinAppointmentDate)) 
AvgAllocationWaitTime 

from OrganisationAreas oa 
JOIN Clients C 
    ON OA.OrganisationAreaId = C.OrganisationAreaId 
JOIN IaptReferrals R 
    ON C.ClientId = R.ClientId 
join (
select referral_iaptReferralId refid 
, min(appointmentdate) MinAppointmentDate 
from IaptAppointments 
where clause goes here 
group by referral_iaptReferralId 
) temp on refid = r.iaptReferralId 

where clause goes here 

保持此基本結構。如果您還想預約,請儘量保持簡單。

+0

嗨丹。謝謝您的幫助。我使用外連接,因爲我仍然希望區域被返回,無論是否有任何引用分配給它,所以我可以顯示零計數。歡呼幫助的人。我會盡量讓我的頭腦圍繞CTE功能,看起來很強大。 – CheGuevarasBeret 2013-04-23 08:10:47

相關問題