google sheets - Cannot use WHERE together with other formatting options - Stack Overflow

I'm using Google Sheet's QUERY function to import data from another tab, add two additional c

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
Share Improve this question edited Mar 25 at 9:08 TheMaster 51.2k7 gold badges72 silver badges99 bronze badges asked Mar 24 at 21:05 martin schwartzmartin schwartz 9818 silver badges17 bronze badges 3
  • 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. – Saddles Commented Mar 24 at 21:10
  • 1 Try putting where A = 'Brooklyn' before the label to see if that works. – Saddles Commented Mar 24 at 21:16
  • 1 @Saddles I added the table, and your answer is correct! Thank you. – martin schwartz Commented Mar 24 at 21:22
Add a comment  | 

2 Answers 2

Reset to default 2

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.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信