0
votes

Having this Google Sheet QUERY formula:

=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)

How to return only one record per email (column Q) being the most recent one looking at date which is at column A?

TEST SHEET:

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

Under tab "results" is the working query where i only need to filter out duplicates based on recency, leaving only the most recent one, and the unique identifier being email

1

1 Answers

2
votes

EDIT

(following OP's shared demo sheet)

The formula to use would be:

=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)

What was changed:

  • We wrapped your original query with the SORTN function to find and exclude all duplicate emails from column Q except the most recent one based on the Timestamp from column A.
  • We -one more time- wrapped the results with the SORT function to have our final results sorted by the Timestamp.

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.

(There is still room for minor improvements/alterations but out of the scope of this question.)


Original answer

Taking for granted that your formula works as expected (cannot check it without a test sheet) but produces multiple rows you can use the limit clause in the end of your formula.

=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)