Pl Sql 中看似简单的查询
我有一个以下格式的“缺陷”表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用途:
id
值的最新日期。id
和date
连接回原始表,以便仅获取最新的行。Use:
id
value.id
anddate
in order to get only the most recent rows.因此,您希望获取每个
id
的最新行,并且仅选择那些打开的行。这是常见的greatest-n-per-group问题的变体。我会这样做:
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:
这应该得到你想要的。我不会有打开和关闭缺陷的记录,而只有跟踪单个缺陷的单个记录。但这可能不是你可以轻易改变的事情。
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.
此查询处理多个打开/关闭/打开,并且只传递一次数据(即没有自连接):
This query handles multiple opens/closes/opens, and only does one pass through the data (i.e. no self-joins):