Using IF in SELECT statement of Google Sheets Query - if-statement

In order to provide relevant data to an accounting person for him to work further in the Bill Booking stage from a master sheet that I manage in Google Sheets, the query function needs to satisfy either of the two conditions.
The master sheet is here
https://docs.google.com/spreadsheets/d/1pY53-XaGnUQ3BPmLh90mLSqIwSo7S2_QOPbD6JBQHOA/edit#gid=0
Two conditions for an accounting person to see what he needs to work on is
(if either of the mentioned two conditions is fulfilled, the data needs to be shown)
If Col8 = "Yes" and Col14 = "Done"
If Col8 = "No" and Col11 = "Done"
For this, I have tried the following
Ifna(QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1pY53-XaGnUQ3BPmLh90mLSqIwSo7S2_QOPbD6JBQHOA/edit#gid=0","Master!A3:N")," Select Col1, Col2, Col3, Col4, Col5, Col7 "&IF(OR(AND(COL8 = "Yes", COL14 = "Done"), AND(COL8 = "No", COL11 = "Done")))&" "))
Currently, this is not showing me any data and without any error. However, it should show him 3 data to work on.
Kindly help me with this.

You need to put the AND and OR directly into the query like this:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1pY53-XaGnUQ3BPmLh90mLSqIwSo7S2_QOPbD6JBQHOA/edit#gid=0","Master!A3:N")," Select Col1, Col2, Col3, Col4, Col5, Col7,Col8,Col11,Col14 where Col8='Yes' and Col14='Done' or Col8='No' and Col11='Done' ")
If you take the IFNA out of the original query, you can see that there is an error because the If statement delivers #N/A. The reason why the If statement delivers #N/A is that there is no second argument to the if statement.
If you reduce the If statement to
=OR(AND(COL8 = "Yes", COL14 = "Done"), AND(COL8 = "No", COL11 = "Done"))
it always delivers FALSE because COL8, COL11 and COL14 are valid cell references, but point to blank cells outside the range of the current sheet so it still doesn't work.

Related

Showing Top 5 Clients at a dashboard

I want to build a dashboard where I can select a year and a application and want to have the top 5 clients shown up.
I build a example Sheet ofr showing:
https://docs.google.com/spreadsheets/d/13yk_SIsv52bZbskOkEzkz4f1aOMnb5yLDk2oo5BawXs/edit?usp=sharing
On the left side I have listed all top clients ob every year and per application.
On the right side I have a little selection tool and I want in the yellow marked area the correct data from the left side.
Thanks for your help
use:
=INDEX(QUERY({""&A1:B\ C1:E};
"select Col3,Col4,Col5
where 3=3 "&
IF(I3="";;" and Col1 contains '"&I3&"'")&
IF(I4="";;" and Col2 contains '"&I4&"'")&
"order by Col5 desc
limit 5"; ))
Maybe something like this:
= Query(A1:E; "Select C,D,E
where A contains '"&I3&"'
and B contains '"&I4&"'
Order by E Desc Limit 5 ")

RelNode of a query in which FROM clause itself has a query

I want to achieve result from a table where I ORDER BY column id and I don't want id to be present in the result. I can achieve this using the following query.
SELECT COALESCE (col1, '**')
FROM (select col1, id FROM myDataSet.myTable WHERE col4 = 'some filter' ORDER BY id);
Now, I want to create a RelNode of the above query. As far as I know, in calcite, to perform table scan, there are only two methods scan(String tableName) and scan(Iterable<String> tableNames). Is there a way to scan(RelNode ) ? How to do this ?
The query
select col1, col2, col2 FROM myDataSet.myTable WHERE col4 = 'some filter' ORDER BY id
should also give you the desired result.
If you want to represent the query you have written more directly, you would start by constructing a RelNode for the query in the from clause, starting with a scan of myDataSet.myTable, adding the filter, and the order. Then you can project the specific set of columns you want.
Just simply create a RelNode of inner subquery and create another projection on top of it. Like so.
builder.scan('myTable')
.filter(builder.call(SqlStdOperator.EQUALS, builder.field(col4), builder.literal('some filter') )))
.project(builder.field('col1'), builder.field('id'))
.sort(builder.field('id'))
.project(builder.call(SqlStdOperator.COALESCE(builder.field('col1'), builder.literal('**'))))
.build()

Insert range using IF formula in SUMIF function with multiple criteria (using arrayformula)

I am using arrayformula with my sumif function which has several criterion and ranges to expand automatically across a set column (B). The formula below works perfectly:
=ARRAYFORMULA(SUMIF('DATA'!$N:$N&'DATA'!$U:$U&'DATA'!$V:$V&'DATA'!$BV:$BV,$B$6:$B&$E$30&$D$26&$C$36,'DATA'!$AG:$AG).
I want to make it more dynamic and insert an IF statement to actually refer to a range vs another based on certain conditions. For ex,
IF($A$6:$A=XX,'DATA'!$N:$N,'DATA'!$M:$M).
The formula then breaks if I do this
=ARRAYFORMULA(SUMIF(IF($A$6:$A=XX,'DATA'!$N:$N,'DATA'!$M:$M)&'DATA'!$U:$U&'DATA'!$V:$V&'DATA'!$BV:$BV,$B$6:$B&$E$30&$D$26&$C$36,'DATA'!$AG:$AG).
Any way/workaround to make it work? I tried multiple times, read multiple forums but can't find an answer.
if XX is not named range it should be in quotes
try:
=ARRAYFORMULA(SUM(IF(
IF(A6="XX", 'DATA'!N:N, 'DATA'!M:M)&DATA!U:U&DATA!V:V&DATA!BV:BV=B6:B&E30&D26&C36,
DATA!AG:AG, )))
UPDATE:
=ARRAYFORMULA(IFNA(VLOOKUP(B7:B&C7:C,
QUERY({DATA!A2:A&DATA!A1, DATA!C2:F; DATA!B2:B&DATA!B1, DATA!C2:F},
"select Col1,sum(Col5)
where Col2 = '"&D4&"'
and Col4 = '"&D3&"'
group by Col1
label sum(Col5)''"), 2, 0)))

Filter data (Not In) based on condition within two data sets in Google sheet

Have employee data set with status active and relieved. And the second data set has the time they have reported to the office. Trying to achieve the names of employee who are absent for the day using one formula. I have attached the images for easy understanding. Please find the sheet wherein I have created sample data and formula which I have used. Trying to achieve it using one array formula or query
https://docs.google.com/spreadsheets/d/1Dj7agceCBS_aCm2GVm4EQHqS8_wQYifoKLMcA0fv4fM/edit?usp=sharing
Have achieved it using Filter and Match
{"Absent For the Day";FILTER((query(importrange("1Dj7agceCBS_aCm2GVm4EQHqS8_wQYifoKLMcA0fv4fM","empdata!A2:B30"),"select Col1 where Col2='Active'")),ISERROR(MATCH((query(importrange("1Dj7agceCBS_aCm2GVm4EQHqS8_wQYifoKLMcA0fv4fM","empdata!A2:B30"),"select Col1 where Col2='Active'")),A2:A20,0)))}
try:
=ARRAYFORMULA({"Absent For the Day", "";
SPLIT(FILTER(empdata!A2:A, empdata!B2:B="active",
NOT(REGEXMATCH(empdata!A2:A, TEXTJOIN("|", 1, A2:A))))&"♦Absent", "♦")})
try:
=ARRAYFORMULA({"Absent For the Day", "";
SPLIT(QUERY(IMPORTRANGE("1Dj7agceCBS_aCm2GVm4EQHqS8_wQYifoKLMcA0fv4fM", "empdata!A2:B"),
"select Col1
where lower(Col2) = 'active'
and not Col1 matches '"&TEXTJOIN("|", 1, A2:A)&"'", 0)&"♦Absent", "♦")})

sqlalchemy double quoting list items

I'm have a quoting issue with raw query when using "WHERE IN" statement. SQLAlchemy is adding double quotes around single quotes in a list...
Query that i'm trying to execute
sql_query = "SELECT col1, col2, col3 FROM preferences WHERE recipient IN :recipients"
preferences = sqlsession.execute(sql_query,dict(recipient=tuple(message.recipients))
message.recipients is a list like so:
["recipient1","recipient2","recipient3"]
SQLAlchemy debug log
INFO:sqlalchemy.engine.base.Engine:SELECT col1, col2, col2 FROM preferences WHERE recipient IN %s
INFO:sqlalchemy.engine.base.Engine:(('recipient1', 'recipient2', 'recipient3'),)
Mariadb log
9 Query SELECT col1, col2, col3 FROM preferences WHERE recipient IN ("'recipient1'", "'recipient1'", "'recipient1'") <-- double quotes around single quotes
I have run strace to see where those quotes are added and it's sqlalchemy fault.
Table schema:
CREATE TABLE `preferences` (
`recipient` varchar(255) COLLATE latin1_general_ci NOT NULL,
`col1` tinyint(1) NOT NULL DEFAULT '1',
`col2` tinyint(1) NOT NULL DEFAULT '1',
`col3` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`recipient`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
Environment
CentOS 7
python-sqlalchemy.x86_64 0.9.7-3.el7 epel
Hi.
As proposed on the gmane.comp.python.sqlalchemy.user, you could use the autoload feature.
To summarise Michael Bayer answer:
t = sqlalchemy.Table(
'preferences', # your table name
sqlalchemy.MetaData(),
autoload=True,
autoload_with=sqlsession,
)
query = sqlalchemy.select([t.c.col1, t.c.col2, t.c.col3]) \
.where(t.c.recipient.in_(message.recipients))
preferences = query.fetchall()
In my case I would had to "autoload" a bunch of tables, and it was not really convenient as they had complex joins.
I ended using something in the lines:
query = "SELECT col1, col2, col3 FROM preferences\nWHERE recipient IN (%s);" % (
', '.join(['%s'] * len(message.recipients)
)
result = sqlsession.execute(query, (message.recipients,)) # notice the ","
The idea is to build the query with the number of items which will be given to the IN expression; by doing so you will have the benefits of the auto-escaping functionality and be compatible with all databases backends (as far as I know).
You can see the resulting query with:
>>> print result._saved_cursor._last_executed
SELECT col1, col2, col3 FROM preferences
WHERE recipient IN ('recipient1', 'recipient2', ...);