gpt4 book ai didi

SQL 选择前几行的条件

转载 作者:行者123 更新时间:2023-12-02 07:42:26 25 4
gpt4 key购买 nike

如何写这样的选择:

SELECT filename, username, date_time, field1, field2... FROM... JOIN... WHERE...
UNLESS user_downloaded_this_file_today_already

我想忽略同一用户在同一天下载的文件。数据示例:

12 | file1.jpg | barney | 2012-03-15 12:50:10 | ...
13 | file1.jpg | roger | 2012-03-15 13:50:10 | ...
14 | file2.jpg | barney | 2012-03-15 14:50:10 | ...
15 | file1.jpg | barney | 2012-03-15 15:50:10 | ...

如何编写忽略第 4 行的 SELECT?相同的文件名,相同的用户,日期差异 < 1。这真的可能吗?

最佳答案

为了在当天允许重复,我认为以下内容就足够了:

select filename, username, date_time, field1, field2
from yourtable t
where not exists (
select 1
from yourtable
where t.filename = filename
and t.username = username
and trunc(t.date_time) = trunc(date_time)
and t.date_time > date_time)
order by t.date_time

这将返回每天每个文件/用户组合的第一个请求。

关于SQL 选择前几行的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9719189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com