0

I'm using Google Sheet's QUERY function to import data from another tab, add two additional column 'Year' and 'Month' which are generated using column H

=QUERY(earnings!A:J, "SELECT A, B, C, YEAR(C), toDate(C) label YEAR(C) 'Year', toDate(C) 'Month' format toDate(C) 'MMM'", 1)

I want to filter the results to only show certain rows but I get an error when trying to add a WHERE clause to this query.

For example:

=QUERY(earnings!A:J, "SELECT A, B, C, YEAR(C), toDate(C) label YEAR(C) 'Year', toDate(C) 'Month' format toDate(C) 'MMM' where A = 'Brooklyn'", 1)

Returns an error

Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "where" "where "" at line 1, column 120. Was expecting one of: "options" ... "," ...

I've tried placing the WHERE clause different places but couldn't get it to work properly. How can I add it to this query?

Here's how my data looks like:

Area Amount Date
Brooklyn $500 01/02/2025
Chicago $600 01/03/2025
Huston $700 01/04/2025
2
  • 1
    Kindly share a minimal, reproducible example—no sensitive data with your desired output—that the community can use to replicate and see what you'd like to do. You may use Tables or tools like Table to Markdown that can easily be copied and pasted. If necessary, as a last resort, Make an anonymous sample document. Commented Mar 24 at 21:10
  • 1
    @Saddles I added the table, and your answer is correct! Thank you. Commented Mar 24 at 21:22

2 Answers 2

3

The where A = 'Brooklyn' should be placed before the label for the query statement to work.

As per Language Clauses:

The syntax of the query language is composed of the following clauses. Each clause starts with one or two keywords. All clauses are optional. Clauses are separated by spaces. The order of the clauses must be as follows:

Clause Usage
select Selects which columns to return, and in what order. If omitted, all of the table's columns are returned, in their default order.
where Returns only rows that match a condition. If omitted, all rows are returned.
group by Aggregates values across rows.
pivot Transforms distinct values in columns into new columns.
order by Sorts rows by values in columns.
limit Limits the number of returned rows.
offset Skips a given number of first rows.
label Sets column labels.
format Formats the values in certain columns using given formatting patterns.
options Sets additional options.
from The from clause has been eliminated from the language.

Note: This hierarchy is from top to bottom, starting from select to options.

Sign up to request clarification or add additional context in comments.

Comments

1

As @Saddles pointed out in the comments, the WHERE clause should go before the lablel like this =QUERY(earnings!A:J, "SELECT A, B, C, YEAR(C), toDate(C) where A = 'Brooklyn' label YEAR(C) 'Year', toDate(C) 'Month' format toDate(C) 'MMM'", 1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.