使用SQL统计某个时间某个状态的案例

发布于 2024-08-09 01:12:45 字数 178 浏览 5 评论 0原文

我需要开发一个查询来计算每月“未结”案件的总数。 我有一个带有 id 和名称的“cases”表,以及一个带有日期时间列、caseid 列和状态的“state_changes”表。

如何计算每个月过去有状态为“开放”的记录但没有相应的状态为“关闭”的记录的案例数量?

我正在使用 SQL Server 2000。

I need to develop a query that will count the total number of 'open' cases per month.
I have a 'cases' table with an id and a name, and a 'state_changes' table with a datetime column, a caseid column and a state.

How can I calculate the number of cases in each month that have a record with state 'open' in the past, but without a corresponding record with state closed?

I'm using SQL server 2000.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2024-08-16 01:12:46

为打开事件创建一个状态更改表查询,为关闭事件创建一个状态更改表查询。

创建一个查询,对案例 ID 进行打开到关闭的外连接,返回两个查询的案例 ID

查询后一个查询结果,以查找“关闭”事件查询中的 ID 为 null 的

行数后者的查询结果。

非常粗略地类似于(我的脑海中,没有更正):

SELECT COUNT (T1.CaseID) FROM (SELECT T1.CaseID AS T1_CaseID, T2.CaseID AS T2_CaseID 
FROM ((SELECT CaseID FROM state_changes WHERE state = 'open' AND timestamp BETWEEN 1-Jan-09 AND 30-Jan-09) AS T1 OUTER JOIN (SELECT CaseID FROM state_changes WHERE state = 'closed' AND timestamp BETWEEN 1-Jan-09 AND 30-Jan-09) AS T2 ON T1.CaseID = T2.CaseID)) WHERE T2_CaseID = NULL

Create a query of the state changes tables for open events and one for close events.

Create a query that does an outer join of the open to the closed on the case ID returning the case ID from both queries

Query the latter query result for rows where the ID from the "close" event query is null

Count the number of rows in the latter query result.

Something very roughly like (off the top of my head, without correction):

SELECT COUNT (T1.CaseID) FROM (SELECT T1.CaseID AS T1_CaseID, T2.CaseID AS T2_CaseID 
FROM ((SELECT CaseID FROM state_changes WHERE state = 'open' AND timestamp BETWEEN 1-Jan-09 AND 30-Jan-09) AS T1 OUTER JOIN (SELECT CaseID FROM state_changes WHERE state = 'closed' AND timestamp BETWEEN 1-Jan-09 AND 30-Jan-09) AS T2 ON T1.CaseID = T2.CaseID)) WHERE T2_CaseID = NULL
久随 2024-08-16 01:12:45

这应该会让您接近(T-SQL):

SELECT
  MONTH(s.casedate) m,
  YEAR(s.casedate)  y,
  COUNT(DISTINCT c.caseid)   count_cases
FROM
  cases c
  INNER JOIN state_changes s ON s.caseid = c.caseid
WHERE
  s.state = 'open'           /* "with state 'open'" */
  AND s.casedate < GETDATE() /* "in the past" */
  AND NOT EXISTS (           /* "without corresp. record with state 'closed'" */
    SELECT 1 FROM state_changes i WHERE i.caseid = s.caseid AND i.state = 'closed'
  )
GROUP BY
  MONTH(s.casedate),
  YEAR(s.casedate)

编辑:要对所有十二个月进行统计(独立于这些月份中存在的实际案例),您需要一个小的辅助表(我们称之为) ,它只包含一列(我们也称其为月份),其中的数字为 1 到 12。然后您将其加入:

SELECT
  m.month,
  COUNT(DISTINCT c.caseid) count_cases
FROM
  cases c
  INNER JOIN state_changes s ON s.caseid = c.caseid
  LEFT  JOIN month         m ON m.month  = MONTH(s.casedate)
WHERE
  s.state = 'open'
  AND YEAR(c.createddate) = YEAR(GETDATE())  /* whatever */
  AND NOT EXISTS (
    SELECT 1 FROM state_changes i WHERE i.caseid = s.caseid AND i.state = 'closed'
  )
GROUP BY
  m.month
ORDER BY
  m.month

This should get you close (T-SQL):

SELECT
  MONTH(s.casedate) m,
  YEAR(s.casedate)  y,
  COUNT(DISTINCT c.caseid)   count_cases
FROM
  cases c
  INNER JOIN state_changes s ON s.caseid = c.caseid
WHERE
  s.state = 'open'           /* "with state 'open'" */
  AND s.casedate < GETDATE() /* "in the past" */
  AND NOT EXISTS (           /* "without corresp. record with state 'closed'" */
    SELECT 1 FROM state_changes i WHERE i.caseid = s.caseid AND i.state = 'closed'
  )
GROUP BY
  MONTH(s.casedate),
  YEAR(s.casedate)

EDIT: To make a statistic over all twelve months (independent of actual cases existing in these months) you need a small helper table (let's call it month), that contains nothing but one column (let's call that month as well) with numbers from 1 to 12. Then you join against it:

SELECT
  m.month,
  COUNT(DISTINCT c.caseid) count_cases
FROM
  cases c
  INNER JOIN state_changes s ON s.caseid = c.caseid
  LEFT  JOIN month         m ON m.month  = MONTH(s.casedate)
WHERE
  s.state = 'open'
  AND YEAR(c.createddate) = YEAR(GETDATE())  /* whatever */
  AND NOT EXISTS (
    SELECT 1 FROM state_changes i WHERE i.caseid = s.caseid AND i.state = 'closed'
  )
GROUP BY
  m.month
ORDER BY
  m.month
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文