基于ID和日期的每个患者遭遇的先前状态和计数

时间:2017-06-14 11:57:55

标签: sql sql-server

我有关于客户预约历史的以下详细信息:

**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

1 个答案:

答案 0 :(得分: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]

将为您提供您正在等待的结果。

此外,我建议您使用技术主键,以避免计算中忘记具有相同CustomerID / Date的条目。一个简单的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]