如何找到每组数据的最新行

发布于 2024-09-01 21:04:24 字数 1830 浏览 9 评论 0原文

我有一个棘手的问题,我正在尝试找到最有效的方法来解决。

这是我的视图结构的简化版本。


表:审核

AuditID | PublicationID | AuditEndDate | AuditStartDate
1       | 3             | 13/05/2010   | 01/01/2010
2       | 1             | 31/12/2009   | 01/10/2009
3       | 3             | 31/03/2010   | 01/01/2010
4       | 3             | 31/12/2009   | 01/10/2009
5       | 2             | 31/03/2010   | 01/01/2010
6       | 2             | 31/12/2009   | 01/10/2009
7       | 1             | 30/09/2009   | 01/01/2009 

我需要从中进行 3 个查询。我需要一个来获取所有数据。接下来只获取历史数据(即除了排除 AuditEndDate 的最新数据项之外的所有数据),然后最后一个查询是获取最新的数据项(AuditEndDate 的最新数据项)。

我有一个日期限制(这是基于每个用户/组的),其中某些用户组只能在某些日期之间看到,这增加了一层复杂性。您会在 where 子句中注意到这一点,即 AuditEndDate<=blah 和 AuditStartDate>=blah

Foreach 发布,选择所有可用数据。

select * from Audits
Where auditEndDate<='31/03/10' and AuditStartDate>='06/06/2009';

对于每个发布,选择所有数据,但排除可用的最新数据(按 AuditEndDate)

select * from Audits 
left join 
(select AuditId as aid, publicationID as pid
   and max(auditEndDate) as pend 
 from Audit where auditenddate <= '31/03/2009' /* user restrict */
 group by pid) Ax 
on Ax.pid=Audit.pubid
where pend!=Audits.auditenddate
AND auditEndDate<='31/03/10' 
and AuditStartDate>='06/06/2009' /* user restrict */

对于每个发布,仅选择可用的最新数据(按 AuditEndDate)

select * from Audits 
left join 
  (select AuditId as aid, publicationID as pid
    and max(auditEndDate) as pend 
   from Audit where auditenddate <= '31/03/2009'/* user restrict */
   group by pid) Ax 
on Ax.pid=Audit.pubid
where pend=Audits.auditenddate
AND auditEndDate<='31/03/10' 
and AuditStartDate>='06/06/2009' /* user restrict */

因此,目前,查询 1 和 3 工作正常,但查询 2 仅返回所有数据而不是限制。

谁能帮助我吗?

谢谢杰森

I have a tricky problem that I'm trying to find the most effective method to solve.

Here's a simplified version of my View structure.


Table: Audits

AuditID | PublicationID | AuditEndDate | AuditStartDate
1       | 3             | 13/05/2010   | 01/01/2010
2       | 1             | 31/12/2009   | 01/10/2009
3       | 3             | 31/03/2010   | 01/01/2010
4       | 3             | 31/12/2009   | 01/10/2009
5       | 2             | 31/03/2010   | 01/01/2010
6       | 2             | 31/12/2009   | 01/10/2009
7       | 1             | 30/09/2009   | 01/01/2009 

There's 3 query's that I need from this. I need to one to get all the data. The next to get only the history data (that is, everything but exclude the latest data item by AuditEndDate) and then the last query is to obtain the latest data item (by AuditEndDate).

There's an added layer of complexity that I have a date restriction (This is on a per user/group basis) where certain user groups can only see between certain dates. You'll notice this in the where clause as AuditEndDate<=blah and AuditStartDate>=blah

Foreach publication, select all the data available.

select * from Audits
Where auditEndDate<='31/03/10' and AuditStartDate>='06/06/2009';

Foreach publication, select all the data but Exclude the latest data available (by AuditEndDate)

select * from Audits 
left join 
(select AuditId as aid, publicationID as pid
   and max(auditEndDate) as pend 
 from Audit where auditenddate <= '31/03/2009' /* user restrict */
 group by pid) Ax 
on Ax.pid=Audit.pubid
where pend!=Audits.auditenddate
AND auditEndDate<='31/03/10' 
and AuditStartDate>='06/06/2009' /* user restrict */

Foreach publication, select only the latest data available (by AuditEndDate)

select * from Audits 
left join 
  (select AuditId as aid, publicationID as pid
    and max(auditEndDate) as pend 
   from Audit where auditenddate <= '31/03/2009'/* user restrict */
   group by pid) Ax 
on Ax.pid=Audit.pubid
where pend=Audits.auditenddate
AND auditEndDate<='31/03/10' 
and AuditStartDate>='06/06/2009' /* user restrict */

So at the moment, query 1 and 3 work fine, but query 2 just returns all the data instead of the restriction.

Can anyone help me?

Thanks

jason

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

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

发布评论

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

评论(1

软糯酥胸 2024-09-08 21:04:24

好吧,实际上......这些查询工作得很好。这是我运行它们的条件 PHP。
一切都很好:)

任何人都可以为此提出更有效的方法,尽管我有兴趣听到这一点。我最初在 where 子句中使用子查询,这运行得非常慢......从 2 秒变成了 50 秒......

Okay, actually... Those queries work fine. It was my conditional PHP for running them.
All good :)

Anyone who can suggest a more efficient method for this though I'd be interested in hearing that. I originally used sub queries in the where clause, and this ran really slow... from 2 seconds it went to 50 seconds....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文