Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

Multiple IF statements with a drop down Google Sheets Query

Consider the following,

I have three columns,

date, sales rep, revenue,

Date,   Sales Rep,    Revenue,
1/1/18  Jon           £250
1/1/18  Evie          £350
1/1/18  Muhammad      £220

Now, to query this data I can do the following:

IF(a2="","", QUERY(B2:C4, "Select * Where A = '"&A2&"') 

now what I want to know is how one would nest multiple queries into one cell if one had multiple drop downs for the above. One selecting sales rep and one for date. So if one Selected a Sales rep & a date, they would get the specific date and sales rep but if one selected JUST a sales rep they would get all their sales regardless of date, and the reverse if possible.

I've tried an OR statement

IF(OR(a2="","", QUERY(B2:C4, "Select * Where A = '"&A2&"'), IF(a3="","", QUERY(B2:C4, "Select * Where b = '"&A3&"'))) but this didn't work. 

I've created some dummy data and an open sheet for your convenience.

https://docs.google.com/spreadsheets/d/15nlBPM9u3g0XNE3xbBgSZJxyHz2JjP9io1LfqGgX4CI/edit?usp=sharing

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is easier to work on the SQL clause a piece at a time and then pull it together, I think. So for instance in J2 you could put

=if(ISBLANK(E2),"","B = '"&E2&"'")

which is either empty or a piece of the where clause.

In J3 do

=if(ISBLANK(E3),"","A = Date'"&text(datevalue(E3),"yyyy-mm-dd")&"'")

then J4 gets

=TEXTJOIN(" AND ",true,J2:J3)

and clinch with J5 as follows.

=if(isBlank(J4),"",query(A:C,"SELECT * WHERE " & J4))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...