2017-06-14 51 views
0

每個病人就診次數我對客戶的預約歷史以下細節:之前的狀態,並根據ID和日期

**CustomerID Date Status** 
123  1/3/2017 Arrived 
123  1/9/2017 Not Arrived 
123  2/1/2017 Canceled 
123  2/25/2017 Arrived 
234  10/8/2016 Arrived 
234  11/3/2016 Not Arrived 
234  1/8/2017 Not Arrived 
234  1/8/2017 Not Arrived 
234  1/18/2017 Canceled 

我將如何使用SQL,計算每次遇到每個約會狀態的總和(已到達,取消,並沒有到達)不包括目前的遭遇?另外,我如何弄清楚每個客戶的預約狀態是什麼? Linked是我試圖製作的屏幕截圖。先謝謝您的幫助!

Desired Results

+0

請仔細閱讀http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of- code-on-so-when-asking-question-285557和接受的答案 –

+0

我正在使用MSSQL ...並感謝您分享鏈接和信息。 – Tmike21

回答

0

下面的查詢:

SELECT appointment.[CustomerID] 
     ,appointment.[Date] 
     ,appointment.[Status] 
     ,(SELECT COUNT(*) FROM [CustomerAppointment] as arrived 
     WHERE arrived.CustomerID = appointment.CustomerID 
     AND arrived.Date < appointment.Date 
     AND arrived.Status = 'Arrived') as [ArrivedSum] 
     ,(SELECT COUNT(*) FROM [CustomerAppointment] as arrived 
     WHERE arrived.CustomerID = appointment.CustomerID 
     AND arrived.Date < appointment.Date 
     AND arrived.Status = 'Canceled') as [CanceledSum] 
     ,(SELECT COUNT(*) FROM [CustomerAppointment] as arrived 
     WHERE arrived.CustomerID = appointment.CustomerID 
     AND arrived.Date < appointment.Date 
     AND arrived.Status = 'Not Arrived') as [NotArrivedSum] 
     ,(SELECT COUNT(*) FROM [CustomerAppointment] as arrived 
     WHERE arrived.CustomerID = appointment.CustomerID 
     AND arrived.Date < appointment.Date) as [TotalAppointments] 
     ,CASE WHEN (SELECT COUNT(*) FROM [CustomerAppointment] as arrived 
     WHERE arrived.CustomerID = appointment.CustomerID 
     AND arrived.Date < appointment.Date) = 0 THEN 'First' ELSE appointment.[Status] END as [LastApptStatus] 
    FROM [dbo].[CustomerAppointment] as appointment 
    GROUP BY appointment.[CustomerID],appointment.[Date],appointment.[Status] 

會給你你正在等待結果。

另外,我建議你使用技術主鍵,以避免從計算中忘記相同的客戶ID /日期的條目。一個簡單的IDENTITY將會很好。

隨着PK的查詢會是這樣:

SELECT appointment.[CustomerID] 
     ,appointment.[Date] 
     ,appointment.[Status] 
     ,(SELECT COUNT(*) FROM [CustomerAppointment] as arrived 
     WHERE arrived.CustomerID = appointment.CustomerID 
     AND arrived.myPK < appointment.myPK 
     AND arrived.Status = 'Arrived') as [ArrivedSum] 
     ,(SELECT COUNT(*) FROM [CustomerAppointment] as arrived 
     WHERE arrived.CustomerID = appointment.CustomerID 
     AND arrived.myPK < appointment.myPK 
     AND arrived.Status = 'Canceled') as [CanceledSum] 
     ,(SELECT COUNT(*) FROM [CustomerAppointment] as arrived 
     WHERE arrived.CustomerID = appointment.CustomerID 
     AND arrived.myPK < appointment.myPK 
     AND arrived.Status = 'Not Arrived') as [NotArrivedSum] 
     ,(SELECT COUNT(*) FROM [CustomerAppointment] as arrived 
     WHERE arrived.CustomerID = appointment.CustomerID 
     AND arrived.myPK < appointment.myPK) as [TotalAppointments] 
     ,CASE WHEN (SELECT COUNT(*) FROM [CustomerAppointment] as arrived 
     WHERE arrived.CustomerID = appointment.CustomerID 
     AND arrived.myPK < appointment.myPK) = 0 THEN 'First' ELSE appointment.[Status] END as [LastApptStatus] 
    FROM [dbo].[CustomerAppointment] as appointment 
    GROUP BY appointment.[CustomerID],appointment.[Date],appointment.[Status]