简体   繁体   English

JdbcTemplate SELECT 查询以获取唯一行(具有唯一 ID 的每一行存在一次)

[英]JdbcTemplate SELECT query to get unique rows (each row with unique ID is present once)

Anyone willing to help me out with one jdbctemplate query?有人愿意帮助我完成一个 jdbctemplate 查询吗?

Need to get only rows with unique id's but there are duplicates, because other columns, such as date have different values.只需要获取具有唯一 ID 但存在重复的行,因为其他列(例如日期)具有不同的值。 I need to get the max date preferably and the resultset should not have any duplicates.我需要最好获得最大日期,并且结果集不应有任何重复。 :x :X

select files.id, files.popularity, user_clicked_files.last_clicked from files inner join user_clicked_files on files.id = user_clicked_files.file_id where user_clicked_files.last_clicked > ? order by files.popularity desc limit 10

output:输出:

[File [id=1a9227b2-d337-4c4b-a26c-42ed8c94de34, last_clicked='2022-05-30', popularity='8'], 
File [id=1a9227b2-d337-4c4b-a26c-42ed8c94de34, last_clicked='2022-06-03', popularity='8'], 
File [id=61f3860c-22b3-4c24-90bd-98c7f520fad7, last_clicked='2022-06-04', popularity='8'], 
File [id=61f3860c-22b3-4c24-90bd-98c7f520fad7, last_clicked='2022-06-03', popularity='8'], 
File [id=d70ff033-04cb-4205-acfe-2432f66525c2, last_clicked='2022-05-30', popularity='7'], 
File [id=d70ff033-04cb-4205-acfe-2432f66525c2, last_clicked='2022-05-30', popularity='7'], 
File [id=d70ff033-04cb-4205-acfe-2432f66525c2, last_clicked='2022-05-31', popularity='7'], 
File [id=9543b842-d592-46df-a63c-8e7c14791169, last_clicked='2022-06-04', popularity='7'], 
File [id=d70ff033-04cb-4205-acfe-2432f66525c2, last_clicked='2022-05-29', popularity='7'], 
File [id=d70ff033-04cb-4205-acfe-2432f66525c2, last_clicked='2022-06-04', popularity='7']]

This almost works, but not quite.这几乎有效,但不完全有效。 There are duplicates sadly.可悲的是有重复。

Here's the two tables I'm working with.这是我正在使用的两张表。

CREATE TABLE files
(
    id         uuid DEFAULT gen_random_uuid() not null primary key,
    file_name  VARCHAR(255),
    popularity INTEGER
);
CREATE TABLE user_clicked_files
(
    user_id      uuid,
    file_id      uuid,
    last_clicked date,
    PRIMARY KEY (user_id, file_id)
);

PS.: Using PostgreSQL PS.:使用 PostgreSQL

You can use row_number()over() window function with common table expression like below to select most recent clicked rows per id:您可以使用row_number()over()窗口函数和下面的公共表表达式来选择每个 id 最近点击的行:

with cte as
(
  select files.id, files.popularity, user_clicked_files.last_clicked ,ROW_NUMBER()over(partition by files.id order by user_clicked_files.last_clicked desc)rn
  from files 
  inner join user_clicked_files on files.id = user_clicked_files.file_id 
  where user_clicked_files.last_clicked > ? 
)
select id, popularity, last_clicked from cte where rn=1
order by popularity desc limit 10

Figured it out.弄清楚了。 Here's the query.这是查询。

select f.id, f.popularity, x.last_clicked
from files f
join (
  select file_id, max(last_clicked) as last_clicked
  from user_clicked_files
  where last_clicked > ?
  group by file_id) x
on (f.id=x.file_id)
order by f.popularity desc
limit 10

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

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