SQL:仅返回第一次出现的情况

发布于 2024-12-24 17:15:37 字数 783 浏览 3 评论 0原文

我很少使用 SQL,并且在我的存档中找不到任何类似的内容,所以我问这个简单的查询问题:我需要一个查询,其中返回 personID 并且仅返回第一个 seenTime

记录:

seenID | personID | seenTime
   108      3         13:34
   109      2         13:56
   110      3         14:22
   111      3         14:31
   112      4         15:04
   113      2         15:52

想要的结果:

personID | seenTime
   3         13:34
   2         13:56
   4         15:04

这就是我所做的&失败:

SELECT t.attendanceID, t.seenPersonID, t.seenTime
(SELECT ROW_NUMBER() OVER (PARTITION BY seenID ORDER BY seenID) AS RowNo,
seenID,
seenPersonID,
seenTime
FROM personAttendances) t
WHERE t.RowNo=1

PS:注意 SQL CE 4

I seldomly use SQL and I cannot find anything similar in my archive so I'm asking this simple query question: I need a query which one returns personID and only the first seenTime

Records:

seenID | personID | seenTime
   108      3         13:34
   109      2         13:56
   110      3         14:22
   111      3         14:31
   112      4         15:04
   113      2         15:52

Wanted result:

personID | seenTime
   3         13:34
   2         13:56
   4         15:04

That's what I did & failed:

SELECT t.attendanceID, t.seenPersonID, t.seenTime
(SELECT ROW_NUMBER() OVER (PARTITION BY seenID ORDER BY seenID) AS RowNo,
seenID,
seenPersonID,
seenTime
FROM personAttendances) t
WHERE t.RowNo=1

P.S: Notice SQL CE 4

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

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

发布评论

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

评论(5

↙温凉少女 2024-12-31 17:15:37

如果您的 sawTime 随着 sawID 的增加而增加:

select personID, min(seenTime) as seenTime
from personAttendances
group by personID

更新另一种情况:

如果情况并非如此,并且您确实想要与最小 sawID 相对应的 sawTime(假设 sawID 是唯一的):

select a.personID, a.seenTime
from personAttendances as a
    join (
        -- Get the min seenID for each personID
        select personID, min(seenID) as seenID
        from personAttendances
        group by personID
    ) as b on a.personID = b.personID
where a.seenID = b.seenID

If your seenTime increases as seenID increases:

select personID, min(seenTime) as seenTime
from personAttendances
group by personID

Update for another case:

If this is not the case, and you really want the seenTime that corresponds with the minimum seenID (assuming seenID is unique):

select a.personID, a.seenTime
from personAttendances as a
    join (
        -- Get the min seenID for each personID
        select personID, min(seenID) as seenID
        from personAttendances
        group by personID
    ) as b on a.personID = b.personID
where a.seenID = b.seenID
☆獨立☆ 2024-12-31 17:15:37

你把事情搞得太困难了:

select personID, min(seenTime)
from personAttendances
group by personID

You're making it way too difficult:

select personID, min(seenTime)
from personAttendances
group by personID
很糊涂小朋友 2024-12-31 17:15:37

您需要按查看时间而不是按查看 ID 排序:

PARTITION BY seenID ORDER BY seenTime

You need to order by seen time not by seen id:

PARTITION BY seenID ORDER BY seenTime
轻拂→两袖风尘 2024-12-31 17:15:37

将其添加到您的 SQL 中:

and where not exists
    (select 1 from personAttendances t2 
    where t.personID=t2.personID 
    and t2.seenID < t.seenID)

Add this to your SQL:

and where not exists
    (select 1 from personAttendances t2 
    where t.personID=t2.personID 
    and t2.seenID < t.seenID)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文