我有2个表段和摘要。
我想按日期间隔列出节目,并仅显示最新的片段(以粗体显示),因为它是该人的片段的最新副本。
这是我以前在下面得到的查询:
select seg.summ, su.channel, seg.block_id, seg.person, seg.updated_at as segupdate
from segments seg
left join summaries su on seg.summ = su.id
where su.created_at between '2018-06-06' and '2018-07-13'
order by summ, block_id, seg.updated_at desc;
我想要这个:
如何更改查询以仅显示加粗的行?删除旧版本的重复项?
答案 0 :(得分:1)
您可以尝试使用ROW_NUMBER
窗口函数,将行编号设置为order by segupdate DESC
,然后获取rn = 1
最新数据行。
SELECT * FROM
(
SELECT *,ROW_NUMBER() OVER(PARTITION BY summ,channel,block_id,person order by segupdate DESC) rn
FROM (
select seg.summ, su.channel, seg.block_id, seg.person, seg.updated_at as segupdate
from segments seg
left join summaries su on seg.summ = su.id
where su.created_at between '2018-06-06' and '2018-07-13'
) t1
) t1
where rn = 1
答案 1 :(得分:0)
这就是我所做的:
select distinct on (seg.summ, seg.block_id, seg.person) su.channel, seg.updated_at as segupdate
from segments seg
left join summaries su on seg.summ = su.id
where su.created_at between '2018-06-06' and '2018-07-13'
order by summ, block_id, person, seg.updated_at desc;