如何写这样的选择:
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/