基于日期的SQL分组和计数

时间:2016-11-02 09:10:55

标签: sql-server tsql date count grouping

无处可去,看起来如此简单。

测试数据是:

declare @table table(SpellAdminsionDate datetime, SpellDischargeDate datetime, Pat_code varchar(10))
 insert into @table (SpellAdminsionDate, SpellDischargeDate, Pat_code) values('2016-09-12 15:55:00:000','2016-09-19 20:20:00:000','HEY3052275')
 insert into @table(SpellAdminsionDate, SpellDischargeDate, Pat_code) values ('2016-09-07 17:17:00:000','2016-09-17 18:40:00:000','HEY0810155')
 insert into @table(SpellAdminsionDate, SpellDischargeDate, Pat_code) values ('2016-09-14 16:50:00:000','2016-09-17 18:01:00:000','HEY1059266')
 insert into @table(SpellAdminsionDate, SpellDischargeDate, Pat_code) values ('2016-09-15 02:47:00:000','2016-09-15 17:28:00:000','HEY0742883')
 insert into @table(SpellAdminsionDate, SpellDischargeDate, Pat_code) values ('2016-08-27 00:11:00:000','2016-09-14 12:49:00:000','HEY3050628')
 insert into @table(SpellAdminsionDate, SpellDischargeDate, Pat_code) values ('2016-09-10 12:24:00:000','2016-09-13 20:00:00:000','HEY0912392')
 insert into @table(SpellAdminsionDate, SpellDischargeDate, Pat_code) values ('2016-09-12 12:51:00:000','2016-09-13 19:55:00:000','HEY0908691')

 Select * from @table`

下面是我显示相同内容的简单代码:

SELECT c.SpellAdmissionDate, 
       c.SpellDischargeDate,
       c.Pat_Code
FROM [CommDB].[dbo].[vwCivicaSLAM1617Live] c
WHERE c.Hrg_Code like 'VA%'
    and c.Pat_Code like 'HEY%'
ORDER BY c.SpellDischargeDate desc

我所追求的是每天COUNT活跃患者,例如,在该日期取12/09/2016结果为5(基于测试数据)为其他2个凸轮在12日之后。

如果它更容易,我会有一个名为DATE_REFERENCE的日期参考表,其中包含我可以使用的每个日期。

5 个答案:

答案 0 :(得分:0)

这是你想要的吗?

SELECT dr.date,
       (SELECT COUNT(*)
        FROM [CommDB].[dbo].[vwCivicaSLAM1617Live] c
        WHERE dr.date between c.SpellAdmissionDate and c.SpellDischargeDate) as cnt_per_day
FROM DATE_REFERENCE dr

将您想要的过滤器添加到相关查询中。

答案 1 :(得分:0)

您可以通过加入您的日期参考表并按照患者参考的不同来计算

SELECT
 dateRef
,COUNT(DISTINCT Pat_Code) AS PatCount
FROM @table t
RIGHT JOIN @date_reference
ON SpellAdminsionDate <= dateRef
AND SpellDischargeDate >= dateRef
GROUP BY dateRef;

答案 2 :(得分:0)

每个日期的患者活跃计数可以通过(对于此处提供的临时数据)来实现

  SELECT DISTINCT CAST(T1.SPELLADMINSIONDATE AS DATE)
  AdmissionDate,PATIENTS.TotalActive
  FROM @TABLE T1 
  CROSS APPLY (SELECT COUNT(*)TotalActive FROM @TABLE T2 WHERE
  CAST(T2.SPELLADMINSIONDATE AS DATE)<=CAST(T1.SPELLADMINSIONDATE AS DATE))    PATIENTS

答案 3 :(得分:0)

考虑到一天没有患者的可能性,您希望使用您的日期参考表作为患者信息的主要和左侧联接。您没有识别列名,所以我只使用了[datecol]。

SELECT
      d.[datecol]                 the_date
    , count(DISTINCT c.Pat_Code) num_patients
FROM DATE_REFERENCE d
LEFT JOIN [CommDB].[dbo].[vwCivicaSLAM1617Live] c
            ON d.[datecol] BETWEEN c.SpellAdmissionDate AND c.SpellDischargeDate
            AND c.Hrg_Code LIKE 'VA%'
            AND c.Pat_Code LIKE 'HEY%'
GROUP BY 
      d.[datecol]
ORDER BY
      d.[datecol] DESC

我怀疑可能需要更多,但如果没有样本数据和预期结果,很难知道你真正需要什么。

NB。我假设date_reference表中的日期是午夜(00:00:00)或者数据类型是日期(没有小时/分钟等)。

答案 4 :(得分:-1)

我认为您需要将DATE_REFERENCE表带到查询中如果它包含您需要根据它们计算患者的所有日期,但这里有一个样本,您可以如何从此表中获得所需的结果< / p>

  Select DISTINCT c.SpellAdmissionDate, count(c.Pat_code) as PCounter 
  From [CommDB].[dbo].[vwCivicaSLAM1617Live] c
  GROUP BY c.SpellAdmissionDate
  ORDER BY c.SpellAdmissionDate desc