SQL:按连续日期计算记录数,即使某个日期没有记录

标签 sql sql-server sql-server-2008 tsql

我正在使用 SQL Server 2008 R2。

我正在查询一个医院预约空档表,并尝试返回一个列表,其中列出了有多少特定医生的预约空档被标记为已预订,按周数/年分组。

有些周还没有任何预约,但我希望结果列出所有即将到来的周,即使预约空档的数量为零。

我正在寻找的输出是这样的:

-------------------------------------------
Year | Week Number | Number of Booked Slots
-------------------------------------------
2017 |     48      |        10            
2017 |     49      |         0
2017 |     50      |         4
2017 |     51      |         2
2017 |     52      |         0
2018 |      1      |         5

我知道聚合结果的标准选择不会显示记录数为零的那些周,因为没有任何可返回的东西——所以我试图通过使用 cte 首先生成一个列表来解决这个问题所有即将到来的几周。

但是,尽我所能,我无法获得显示零周的查询...

我已经看到许多针对与此类似问题的解决方案,但尽管进行了试验,但我无法将它们应用于我的特定问题(包括 SQL Count to include zero values)

这是迄今为止我编写的查询的最新迭代。

    WITH CTE_Dates AS
    (
    SELECT DISTINCT Slot_Start_Date AS cte_date
    FROM [Outpatients.vw_OP_Clinic_Slots WITH (NOLOCK)
    )

SELECT 
    DATEPART(year,OPCS.Slot_Start_Date) [Year]
    ,DATEPART(week,OPCS.Slot_Start_Date) [Week Number]
    ,count(OPCS.Slot_Start_Date) [Number of Booked Slots]

FROM 
    Outpatients.vw_OP_Clinic_Slots OPCS WITH (NOLOCK)
    LEFT OUTER JOIN CTE_Dates ON OPCS.Slot_Start_Date=CTE_Dates.cte_date
    LEFT OUTER JOIN Outpatients.vw_OP_Clinics CLIN ON OPCS.Clinic_Code=CLIN.Clinic_Code

WHERE
    OPCS.Slot_Start_Date >= '14/08/2017'
    AND OPCS.Booked_Flag = 'Y'
    AND CLIN.Lead_Healthcare_Professional_Name = 'Dr X'

GROUP BY
    DATEPART(year,OPCS.Slot_Start_Date)
    ,DATEPART(week,OPCS.Slot_Start_Date)

ORDER BY 
    DATEPART(year,OPCS.Slot_Start_Date)asc
    ,DATEPART(week,OPCS.Slot_Start_Date)asc

它返回给我的结果是正确的,但我只需要它在列表中包含计数为零的那些周。

谁能解释一下我哪里出错了?我猜我没有正确加入 cte,但我已经尝试了产生相同结果的右连接和左连接。我还尝试通过交换上面的查询语句和 cte 来反转查询,但这也不起作用。

感谢任何人可以建议的任何指导。

最佳答案

只需RIGHT JOIN @ListOfWeeks 表到您拥有的结果集:

DECLARE @ListOfWeeks TABLE ([Week_No] int, [Year_Number] int);

DECLARE @i tinyint = 1, @y int = 2010;

WHILE @i <= 52 AND @y < 2018
BEGIN
    INSERT INTO @ListOfWeeks([Week_No], [Year_Number]) VALUES (@i, @y);

    IF @i = 52 BEGIN
       SET @i = 0
       SET @y +=1
       END
     SET @i += 1
END

SELECT * FROM @ListOfWeeks

WITH [Your_Part] AS(
SELECT 
    DATEPART(year,OPCS.Slot_Start_Date) [Year]
    ,DATEPART(week,OPCS.Slot_Start_Date) [Week Number]
    ,count(OPCS.Slot_Start_Date) [Number of Booked Slots]

FROM 
    Outpatients.vw_OP_Clinic_Slots OPCS WITH (NOLOCK)
    LEFT OUTER JOIN CTE_Dates ON OPCS.Slot_Start_Date=CTE_Dates.cte_date
    LEFT OUTER JOIN Outpatients.vw_OP_Clinics CLIN ON OPCS.Clinic_Code=CLIN.Clinic_Code

WHERE
    OPCS.Slot_Start_Date >= '14/08/2017'
    AND OPCS.Booked_Flag = 'Y'
    AND CLIN.Lead_Healthcare_Professional_Name = 'Dr X'

GROUP BY
    DATEPART(year,OPCS.Slot_Start_Date)
    ,DATEPART(week,OPCS.Slot_Start_Date)
),
SELECT xxx.[Year_Number], xxx.[Week_No], yp.[Number of Booked Slots]
FROM [Your_Part] yp
RIGHT JOIN @ListOfWeeks xxx ON  yp.[Year] = xxx.[Year_Number] AND yp.[Week Number] = xxx.[Week_No]

关于SQL:按连续日期计算记录数,即使某个日期没有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45733317/

相关文章:

sql - 字段值内的分隔符 SQL Server

SQL 查询 JOIN 或 IN 运算符?

sql-server-2008 - 创建游标以遍历记录的SQL Server 2008请求示例

php - 如何忽略 MySQL 查询中 JOIN 表的结果

c# - 系统.Data.SqlClient。安装后无法使用

php - 不同类型的用户重定向到具有不同内容的同一页面(index.php)

sql - 测试 MySQL 表中是否存在行的最佳方法

SQL Server DATEADD 空值错误行为

sql - 我该如何为此编写sql函数?

sql-server - '>'附近的语法不正确。在案件陈述中-无法弄清原因。有什么我想念的吗? SQL Server 2008