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 |
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 Answers
Reset to default 2The 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.
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)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744228615a4564131.html
where A = 'Brooklyn'
before the label to see if that works. – Saddles Commented Mar 24 at 21:16