QUERY 公式 return 仅基于时间戳的每封电子邮件的最新记录

QUERY formula to return only the latest record per email based on timestamp

有这个 Google Sheet QUERY 公式:

=QUERY(FORMULARIO!A4:Q, " select G,E,K,M,N,L,O,Q where not upper(J) = 'PAGÓ' and A > date '"&TEXTO(HOY()-3, "yyyy-mm-dd")&"' and E is not null and O is not null and Q contains '@' and not Q matches '"&TEXTJOIN("|", 1, QUERY(FORMULARIO!J4:Q, "select Q where upper(J) = 'PAGÓ' ", 0))&"'", 0)

如何 return 每封电子邮件(Q 列)只有一条记录是最近查看 A 列日期的记录?

测试SHEET:

https://docs.google.com/spreadsheets/d/1jBMo42cbylrNHcf9b9QLI4oDw0H0oomCGrEAuPsBlA8/edit#gid=1465616509

在选项卡 "results" 下是工作查询,我只需要根据新近度过滤掉重复项,只留下最近的一个,唯一标识符是电子邮件

编辑

(跟随 OP 的共享演示 sheet)

要使用的公式为:

=SORT(SORTN(QUERY(FORMULARIO!A4:Q, " select G,E,K,M,N,L,O,Q where not upper(J) = 'PAGÓ' and A > datetime '"&TEXTO(AHORA()-3, "yyyy-mm-dd HH:mm:ss")&"' and E is not null and O is not null and Q contains '@' and not Q matches '"&TEXTJOIN("|", 1, QUERY(FORMULARIO!J4:Q, "select Q where upper(J) = 'PAGÓ' ", 0))&"'", 1),9^9,2,2,1),1,1)

更改内容:

  • 我们用 SORTN 函数包装了您的原始查询,以根据列 A 中的时间戳查找并排除列 Q 中的所有重复电子邮件,但最近的电子邮件除外。
  • 我们 - 再一次 - 使用 SORT 函数包装结果,使我们的最终结果按时间戳排序。

Pro tip
An extra change was also made within the main query by changing
and A > date '"&TEXT(HOY()-3, "yyyy-mm-dd")&"'
to
and A > datetime '"&TEXT(AHORA()-3, "yyyy-mm-dd HH:mm:ss")&"'
So we changed TODAY to NOW.

By doing this we count dates making use of both the date and time present in a timestamp instead of just the date.

(minor improvements/alterations还有空间,但不在本题范围内。)


原回答

理所当然地认为你的公式按预期工作(无法在没有测试的情况下检查它sheet)但是产生多行你可以使用limit公式末尾的子句。

=QUERY(FORMULARIO!A4:Q, " select G,E,K,M,N,L,O,Q where not upper(J) = 'PAGÓ' and A > date '"&TEXTO(HOY()-3, "yyyy-mm-dd")&"' and E is not null and O is not null and Q contains '@' and not Q matches '"&TEXTJOIN("|", 1, QUERY(FORMULARIO!J4:Q, "select Q where upper(J) = 'PAGÓ' ", 0))&"' limit 1", 0)