繁体   English   中英

查找具有与最大日期行列匹配值相关的表行?

[英]Find rows of table with related max date row column matching value?

哇,这个问题真的很难简洁地提出。 所以,这是数据:

Person:
+----+---------+
| ID | Name    |
+----+---------+
|  1 | Bob     |
|  2 | Alice   |
|  3 | Greg    |
|  4 | Lisa    |
+----+---------+

Activity:
+----+----------+------------+----------+
| ID | PersonID | Date       | Activity |
+----+----------+------------+----------+
|  1 | 1        | 2017-03-01 | foo      |
|  2 | 1        | 2017-03-02 | bar      |
|  3 | 2        | 2016-12-01 | foo      |
|  4 | 3        | 2017-01-15 | foo      |
+----+----------+------------+----------+

我想返回最近Activityfoo所有Person行。

Return:
+----+---------+
| ID | Name    |
+----+---------+
|  2 | Alice   |
|  3 | Greg    |
+----+---------+

谢谢!

MySQL的

select P3.*
from 
(
select PersonID, max(Date) as mDate
from Activity
group by PersonID
) a1
inner join Activity A2
  on A2.PersonID = A1.PersonID
  and A2.Date = A1.mDate
inner join Person P3
  on P3.ID = A2.PersonID
where A2.Activity = 'Foo'
and not exists (select 1 -- This is for those who did both on one day
                from Activity A4 
                where A4.Activity = 'Bar'
                and A4.PersonID = A1.PersonID
                and A4.Date = A1.mDate)

对于SQL Server / Oracle(很有趣)

with CTE as
(
select A1.*, row_number() over(partition by PersonID order by Date desc) as r_ord
from Activity A1
)
select P2.*
from Person P2
inner join CTE 
  on CTE.PersonID = P2.ID
where CTE.r_ord = 1
and CTE.Activity = 'Foo'
select * from Person where ID in 
(select PersonID from (select Top 1 * from Activity where PersonID = Person.ID order by Activity.Date desc) Tmp where Tmp.Activity <> 'bar')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM