获取每个类别的最新状态

编程入门 行业动态 更新时间:2024-10-18 08:26:41
本文介绍了获取每个类别的最新状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了一个有趣的问题.我有一个名为email_track的表,用于跟踪每个类别(如邀请,新闻稿)的电子邮件状态

I came across this interesting problem. I have a table named email_track to track email status for each category say (invitation, newsletter)

这是我的表格数据的外观,

This is how my table data looks,

通过以下查询,我可以获得每个to_email的最新记录,

With these following queries I'm able to get most recent record for each to_email,

with `et2` as ( select `et1`.`category`, `et1`.`to_email`, `et1`.`subject`, `et1`.`status`, ROW_NUMBER() OVER (partition by `to_email` order by `id` desc) as `rn` from `email_track` `et1` ) select * from `et2` where `rn` = 1;

select `et1`.`category`, `et1`.`to_email`, `et1`.`subject`, `et1`.`status`, `et2`.`id` from `email_track` `et1` left join `email_track` `et2` on (`et1`.`to_email` = `et2`.`to_email` and `et1`.`id` < `et2`.`id`) where `et2`.`id` is null;

我期望的是电子邮件john@example,我应该获得两条记录,一条用于类别邀请,另一条用于新闻通讯.现在,由于我们按to_email进行分区,因此不会得到该结果.

What I'm expecting is for email john@example I should get two records one for category invitation and the other for the newsletter. Now, we won't get that result since we partition by to_email

推荐答案

我应该获得两条记录,一条用于类别邀请,另一条用于新闻通讯.现在,由于我们按to_email进行分区,因此不会得到该结果.

I should get two records one for category invitation and the other for the newsletter. Now, we won't get that result since we partition by to_email.

在窗口函数的partition by子句中添加category应该足以为您提供所需的结果:

Adding the category to the partition by clause of the window function should be enough to give your the result that you want:

with et2 as ( select et1.category, et1.to_email, et1.subject, et1.status, row_number() over(partition by to_email, category order by id desc) as rn from email_track et1 ) select * from et2 where rn = 1;

更多推荐

获取每个类别的最新状态

本文发布于:2023-11-22 05:58:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类别   状态   最新

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!