Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 基于ID和日期的先前状态和每次患者遭遇次数_Sql_Sql Server - Fatal编程技术网

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

Sql 基于ID和日期的先前状态和每次患者遭遇次数,sql,sql-server,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

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

**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计算每次会面(已到达、已取消和未到达,不包括当前会面)的每个约会状态的总和?此外,我如何确定每位客户之前的预约状态?链接是我试图制作的屏幕截图。提前感谢您的帮助

以下查询:

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/日期的条目。一个简单的身份将非常有用

对于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]

请阅读并接受答案我正在使用MSSQL…谢谢分享链接和信息。