特定周内没有截止日期的历史病例数

问题描述 投票:0回答:1

在 SQL Server 中我有两个查询。
查询 1 显示了今年的月份和星期。
查询 2 显示了特定类型的案例,具有独特的案例编号、创建日期、关闭日期和状态。

当案例的状态为“新”或“正在运行”时,关闭日期为 NULL。当案件状态为“已取消”或“已结束”时,将会有一个结束日期。

这是我的两个疑问。

查询1:

SELECT
DISTINCT YEAR(DAG_CODE) AS Jaar,
YEAR(DAG_CODE) * 100 + MONTH(DAG_CODE) AS Maand,
YEAR(DAG_CODE) * 100 + DATEPART(ISO_WEEK, DAG_CODE) AS Weeknr
FROM [firstsource]
WHERE DAG_CODE BETWEEN '2024-01-01 00:00:00' AND GETDATE()

查询2:

SELECT
CaseNumber,
CreatedDate,
ClosedDate,
Status
FROM [secondsource] 
WHERE Createddate >= '2023-01-01 0:00:00'

请注意,查询 1 于 2024 年开始,查询 2 于 2023 年开始。

现在我正在寻找一个查询,它给出从第一个查询到现在的周数,以及在该特定周(从查询 1 开始)星期一上午 0:00 开始时查询 2 的所有病例数的计数: 00 的 Closeddate 仍然为 NULL。

一旦有截止日期,下周就不应计算病例数。但是:我想检索每周仍有多少案件未决的历史概览。

为了显示数据中的内容,以下是两个查询的结果:

第一个查询给出下表:

<table>
<thead>
<tr>
<th>year</th>
<th>month</th>
<th>week</th>
</tr>
</thead>
<tbody>
<tr>
<td>2024</td>
<td>202401</td>
<td>202401</td>
</tr>
<tr>
<td>2024</td>
<td>202401</td>
<td>202402</td>
</tr>
<tr>
<td>2024</td>
<td>202401</td>
<td>202403</td>
</tr>
<tr>
<td>2024</td>
<td>202401</td>
<td>202404</td>
</tr>
<tr>
<td>2024</td>
<td>202401</td>
<td>202405</td>
</tr>
<tr>
<td>2024</td>
<td>202402</td>
<td>202405</td>
</tr>
</tbody>
</table>

第二个查询给出以下结果:

<table>
<thead>
<tr>
<th>Casenumber</th>
<th>Createddate</th>
<th>Closeddate</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>1001</td>
<td>1-12-2023  15:33:44</td>
<td>NULL</td>
<td>Running</td>
</tr>
<tr>
<td>1002</td>
<td>1-12-2023  15:33:44</td>
<td>1-1-2024  09:01:16</td>
<td>Closed</td>
</tr>
<tr>
<td>1003</td>
<td>16-01-1-2024  15:00:33</td>
<td>16-1-2024  09:01:16</td>
<td>Cancelled</td>
</tr>
<tr>
<td>1004</td>
<td>18-01-1-2024  15:00:33</td>
<td>23-1-2024  09:01:16</td>
<td>Closed</td>
</tr>
<tr>
<td>1005</td>
<td>19-01-1-2024  15:00:33</td>
<td>NULL</td>
<td>New</td>
</tr>
</tbody>
</table>
sql-server count comparison
1个回答
0
投票

如果我正确理解您的要求,这看起来像是两个单独的任务。 首先,我们想知道案件开放的周数,直到当前完成的周。

DECLARE @Table TABLE (ID INT IDENTITY, StartDateTime DATETIME, EndDateTime DATETIME);
INSERT INTO @Table (StartDateTime, EndDateTime) VALUES
('2023-12-01', '2024-01-01'),
('2023-11-01', '2024-01-01'),
('2024-01-01', '2024-02-01'),
('2024-01-01', '2024-03-01');

;WITH CTE AS (
SELECT a.ID, a.StartDateTime AS ThisDate, DATEPART(YEAR,a.StartDateTime) AS Year, DATEPART(WEEK,a.StartDateTime) AS Week, a.EndDateTime
  FROM @Table a
 UNION ALL
SELECT a.ID, DATEADD(WEEK,1,a.ThisDate), DATEPART(YEAR,DATEADD(WEEK,1,a.ThisDate)), DATEPART(WEEK,DATEADD(WEEK,1,a.ThisDate)) , a.EndDateTime
  FROM CTE a
 WHERE a.ThisDate < DATEADD(WEEK,-1,a.EndDateTime)
   AND a.ThisDate < DATEADD(WEEK,-1,GETDATE())
)

SELECT c.ID, c.ThisDate, c.Year, c.Week, c.EndDateTime
  FROM CTE c
 ORDER BY c.ID, c.Year, c.Week;
身份证 这个日期 年份 结束日期时间
1 2023-12-01 00:00:00.000 2023 48 2024-01-01 00:00:00.000
1 2023-12-08 00:00:00.000 2023 49 2024-01-01 00:00:00.000
1 2023-12-15 00:00:00.000 2023 50 2024-01-01 00:00:00.000
1 2023-12-22 00:00:00.000 2023 51 2024-01-01 00:00:00.000
1 2023-12-29 00:00:00.000 2023 52 2024-01-01 00:00:00.000
2 2023-11-01 00:00:00.000 2023 44 2024-01-01 00:00:00.000
2 2023-11-08 00:00:00.000 2023 45 2024-01-01 00:00:00.000
2 2023-11-15 00:00:00.000 2023 46 2024-01-01 00:00:00.000
2 2023-11-22 00:00:00.000 2023 47 2024-01-01 00:00:00.000
2 2023-11-29 00:00:00.000 2023 48 2024-01-01 00:00:00.000
2 2023-12-06 00:00:00.000 2023 49 2024-01-01 00:00:00.000
2 2023-12-13 00:00:00.000 2023 50 2024-01-01 00:00:00.000
2 2023-12-20 00:00:00.000 2023 51 2024-01-01 00:00:00.000
2 2023-12-27 00:00:00.000 2023 52 2024-01-01 00:00:00.000
3 2024-01-01 00:00:00.000 2024 1 2024-02-01 00:00:00.000
3 2024-01-08 00:00:00.000 2024 2 2024-02-01 00:00:00.000
3 2024-01-15 00:00:00.000 2024 3 2024-02-01 00:00:00.000
3 2024-01-22 00:00:00.000 2024 4 2024-02-01 00:00:00.000
3 2024-01-29 00:00:00.000 2024 5 2024-02-01 00:00:00.000
4 2024-01-01 00:00:00.000 2024 1 2024-03-01 00:00:00.000
4 2024-01-08 00:00:00.000 2024 2 2024-03-01 00:00:00.000
4 2024-01-15 00:00:00.000 2024 3 2024-03-01 00:00:00.000
4 2024-01-22 00:00:00.000 2024 4 2024-03-01 00:00:00.000
4 2024-01-29 00:00:00.000 2024 5 2024-03-01 00:00:00.000

在这里,我们使用递归 CTE 来查找开始日期和结束日期之前或当前日期之前的一周之间的所有周。

其次,您需要一个开放 ID 的列表(?),每周都有开放的 ID(?)。如果您想将其扩展到任何周,您可能需要包含一个日历表,这样您就不会在根本没有门票的任何周内得到空白。

DECLARE @Table TABLE (ID INT IDENTITY, StartDateTime DATETIME, EndDateTime DATETIME);
INSERT INTO @Table (StartDateTime, EndDateTime) VALUES
('2023-12-01', '2024-01-01'),
('2023-11-01', '2024-01-01'),
('2024-01-01', '2024-02-01'),
('2024-01-01', '2024-03-01');

;WITH CTE AS (
SELECT a.ID, a.StartDateTime AS ThisDate, DATEPART(YEAR,a.StartDateTime) AS Year, DATEPART(WEEK,a.StartDateTime) AS Week, a.EndDateTime
  FROM @Table a
 UNION ALL
SELECT a.ID, DATEADD(WEEK,1,a.ThisDate), DATEPART(YEAR,DATEADD(WEEK,1,a.ThisDate)), DATEPART(WEEK,DATEADD(WEEK,1,a.ThisDate)) , a.EndDateTime
  FROM CTE a
 WHERE a.ThisDate < DATEADD(WEEK,-1,a.EndDateTime)
   AND a.ThisDate < DATEADD(WEEK,-1,GETDATE())
)

SELECT a.Year, a.Week, STRING_AGG(CAST(a.ID AS VARCHAR(10)),', ') WITHIN GROUP (ORDER BY a.ID) AS OpenIDs
  FROM CTE a
 GROUP BY a.Year, a.Week
 ORDER BY a.Year, a.Week;
年份 OpenID
2023 44 2
2023 45 2
2023 46 2
2023 47 2
2023 48 1, 2
2023 49 1, 2
2023 50 1, 2
2023 51 1, 2
2023 52 1, 2
2024 1 3, 4
2024 2 3, 4
2024 3 3, 4
2024 4 3, 4
2024 5 3, 4

这里我们的做法与之前基本相同,但随后按周对结果进行分组,并使用

STRING_AGG
生成该周内打开的 ID 的逗号分隔列表。我添加了可选的
WITHIN GROUP
语法来按顺序返回 ID。

© www.soinside.com 2019 - 2024. All rights reserved.