Pl Sql 中看似简单的查询

发布于 2024-08-17 13:34:58 字数 461 浏览 7 评论 0原文

我有一个以下格式的“缺陷”表:

id   status  stat_date   line   div  area
1    Open    09/21/09    F      A    cube
1    closed  01/01/10    F      A    cube
2    Open    10/23/09    B      C    Back
3    Open    11/08/09    S      B    Front
3    closed  12/12/09    S      B    Front   

我的问题是我想编写一个仅提取“开放”缺陷的查询。如果我编写一个查询来简单地提取所有未解决的缺陷,那么我会得到错误的结果,因为存在一些缺陷, 有 2 条与之关联的记录。例如,通过我编写的查询,即使它们已关闭,我也会在结果中得到缺陷 id#s 1 和 3。我希望我已经很好地解释了我的问题。谢谢。

I have a table "defects" in the following format:

id   status  stat_date   line   div  area
1    Open    09/21/09    F      A    cube
1    closed  01/01/10    F      A    cube
2    Open    10/23/09    B      C    Back
3    Open    11/08/09    S      B    Front
3    closed  12/12/09    S      B    Front   

My problem is that I want to write a query that just extracts the "Open" defects. If I write a query to simply extract all open defects, then I get the wrong result because there are some defects,
that have 2 records associated with it. For example, with the query that I wrote I would get defect id#s 1 and 3 in my result even though they are closed. I hope I have explained my problem well. Thank you.

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

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

发布评论

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

评论(5

萝莉病 2024-08-24 13:34:58

用途:

SELECT t.*
  FROM DEFECTS t
  JOIN (SELECT d.id,
               MAX(d.stat_date) 'msd'
          FROM DEFECTS d
      GROUP BY d.id) x ON x.id = t.id
                      AND x.msd = t.stat_date
 WHERE t.status != 'closed'
  1. 连接获取每个 id 值的最新日期。
  2. 根据 iddate 连接回原始表,以便仅获取最新的行。
  3. 过滤掉那些处于关闭状态的行,以了解当前打开的行

Use:

SELECT t.*
  FROM DEFECTS t
  JOIN (SELECT d.id,
               MAX(d.stat_date) 'msd'
          FROM DEFECTS d
      GROUP BY d.id) x ON x.id = t.id
                      AND x.msd = t.stat_date
 WHERE t.status != 'closed'
  1. The join is getting the most recent date for each id value.
  2. Join back to the original table on based on the id and date in order to get only the most recent rows.
  3. Filter out those rows with the closed status to know the ones that are currently open
执着的年纪 2024-08-24 13:34:58

因此,您希望获取每个 id 的最新行,并且仅选择那些打开的行。这是常见的greatest-n-per-group问题的变体。

我会这样做:

SELECT d1.*
FROM defects d1
LEFT OUTER JOIN defects d2
  ON (d1.id = d2.id AND d1.stat_date < d2.stat_date)
WHERE d2.id IS NULL
  AND d1.status = 'Open';

So you want to get the most recent row per id and of those, only select those that are open. This is a variation of the common greatest-n-per-group problem.

I would do it this way:

SELECT d1.*
FROM defects d1
LEFT OUTER JOIN defects d2
  ON (d1.id = d2.id AND d1.stat_date < d2.stat_date)
WHERE d2.id IS NULL
  AND d1.status = 'Open';
听不够的曲调 2024-08-24 13:34:58
Select * 
from defects d
where status = 'Open'
and not exists (
   select 1 from defects d1
   where d1.status = 'closed'
   and d1.id = d.id
   and d1.stat_date > d.stat_date
)
Select * 
from defects d
where status = 'Open'
and not exists (
   select 1 from defects d1
   where d1.status = 'closed'
   and d1.id = d.id
   and d1.stat_date > d.stat_date
)
浪菊怪哟 2024-08-24 13:34:58

这应该得到你想要的。我不会有打开和关闭缺陷的记录,而只有跟踪单个缺陷的单个记录。但这可能不是你可以轻易改变的事情。

SELECT id FROM defects 
WHERE status = 'OPEN' AND id NOT IN 
(SELECT id FROM defects WHERE status = 'closed')

This should get what you want. I wouldn't have a record for open and closing a defect, rather just a single record to track a single defect. But that may not be something you can change easily.

SELECT id FROM defects 
WHERE status = 'OPEN' AND id NOT IN 
(SELECT id FROM defects WHERE status = 'closed')
污味仙女 2024-08-24 13:34:58

此查询处理多个打开/关闭/打开,并且只传递一次数据(即没有自连接):

SELECT * FROM
(SELECT DISTINCT
        id
       ,FIRST_VALUE(status)
        OVER (PARTITION BY id
              ORDER BY stat_date desc)
        as last_status
       ,FIRST_VALUE(stat_date)
        over (PARTITION BY id
              ORDER BY stat_date desc)
        AS last_stat_date
       ,line
       ,div
       ,area
 FROM defects)
WHERE last_status = 'Open';

This query handles multiple opens/closes/opens, and only does one pass through the data (i.e. no self-joins):

SELECT * FROM
(SELECT DISTINCT
        id
       ,FIRST_VALUE(status)
        OVER (PARTITION BY id
              ORDER BY stat_date desc)
        as last_status
       ,FIRST_VALUE(stat_date)
        over (PARTITION BY id
              ORDER BY stat_date desc)
        AS last_stat_date
       ,line
       ,div
       ,area
 FROM defects)
WHERE last_status = 'Open';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文