Google sheets query in need of a ifs statement - google-sheets-formula

I am trying to select different column depending on the column where a certain string is found.
This is my curent query.
=QUERY(IMPORTRANGE(Configuration!I5,"Employes!A2:Z"),"select Col26, Col3 where Col16 = '"&D27&"' or Col18 = '"&D27&"' or Col20 = '"&D27&"'")
Right now, it brings only 2 columns of data, but I would need it to also bring the columns following the one where it will find the sting "D27".

Try
=query({
QUERY(IMPORTRANGE(Configuration!I5,"Employes!A2:Z"),"select Col26,Col3,Col16,Col17");
QUERY(IMPORTRANGE(Configuration!I5,"Employes!A2:Z"),"select Col26,Col3,Col18,Col19");
QUERY(IMPORTRANGE(Configuration!I5,"Employes!A2:Z"),"select Col26,Col3,Col20,Col21")},
"select Col1,Col2,Col4 where Col3='"&D27&"'")

Related

How to use Google Sheets to create table of Likert scale results grouped by counts

I want to take responses to Likert survey items and create a summary table of the counts of each response (text labels: strongly disagree, disagree, neutral, agree, strongly agree).
(sample data set & manually created desired results table available in this sheet)
I am only able to do one question at a time with QUERY.
=QUERY(likertTable,"select B,count(B) group by B",1)
Creating an array of QUERY functions does not always work when one option is not selected for a question, resulting in mismatched row size.
Is there a way to create the desired table with a single QUERY—or other (e.g., VLOOKUP, COUNTIF)—formula?
Try this, based on the order of criteria F3:F7
=query(arrayformula(split(flatten(B2:D2&"~"&vlookup(B3:D,{$F$3:$F$7,row($F$3:$F$7)},2,0)),"~")),
"select count(Col2) where Col2 is not null group by Col2 pivot Col1")
try:
=ARRAYFORMULA(QUERY(SPLIT(FLATTEN(IF(B3:D="",,B2:D2&"×"&B3:D)), "×"),
"select Col2,count(Col2) where Col2 is not null group by Col2 pivot Col1"))
to sort, add "sort column" manually and pass through another QUERY:
=QUERY({ARRAYFORMULA(QUERY(SPLIT(FLATTEN(IF(B3:D="",,B2:D2&"×"&B3:D)), "×"),
"select Col2,count(Col2) where Col2 is not null group by Col2 pivot Col1")),
{"sort";4;2;3;5;1}},
"select Col1,Col2,Col3,Col4 order by Col5",1)

BIRT allow user to dynamically select report's columns

I want to add an option for the user when creating the report to select the columns that the report will show. See the attached image below on how it is suppose to look.
Is there a way to do that?
I don't know about the parameter dialog, but assuming that your column names are in an array.
You can have an SQL query with all possible column names
(probably you should use special comments to mark the beginning and end of the select list).
E.g.
select
'X' as dummy
-- BEGIN COLS
, column1
, column2
...
-- END COLS
from ...
where ...
order by ...
Then, in the beforeOpen event of the query, you can access and modify the query with this.queryText (IIRC) and remove all those lines ("," + columnname) in the marked part for which columnname is not contained in the array.

Expanding query with where or filtering in formula in google sheets

I have this formula which works great: =ArrayFormula(QUERY(TRANSPOSE(TRIM(SPLIT(JOIN(";",'TAB'!I2:I),";"))&{"";""}),"select Col1, count(Col2) group by Col1 label count(Col2) ''",0))
It takes values in column that are separated with ; and counts unique entries and plots everything in a table.
Question: I would like to add a filter/condition so that it would plot only the values what have specific entries in another column. Like A or B ir C, but not all values.
I have tried: =ArrayFormula(QUERY(TRANSPOSE(TRIM(SPLIT(JOIN(";",'TAB'!I2:I),";"))&{"";""})&'RAW-TODOS'!F2:F,"select Col1 * where 'TAB'!F2:F ='A'or 'B' or 'C', count(Col2) group by Col1 label count(Col2) ''",0))
but probably because of obvious reasons it did not work. Please help me with this one.
Thank you in advance.
try:
=INDEX(QUERY(TRIM(FLATTEN(SPLIT(FILTER('RAW-TODOS'!B:B,
REGEXMATCH('RAW-TODOS'!A:A, "NEW|IN PROGRESS")), ";"))),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''"))
Input the criteria in J1, which is referred by my formula.
leave it blank to match all rows
for single value, simply input it, i.e. NEW
for multiple values, join them with |, i.e. NEW|IN PROGRESS
=ArrayFormula(QUERY(SPLIT(FLATTEN('RAW-TODOS'!A2:A&"♦"&TRIM(SPLIT('RAW-TODOS'!B2:B,";"))),"♦"),CONCATENATE("select Col2,count(Col2) where Col2 is not null",IF(J1="",," and Col1 matches '"&J1&"'")," group by Col2 label count(Col2) ''"),0))

retrieve only unique values via query

Here's the formula I currently use:
=query(IMPORTRANGE("XXXX","XXXXX!A:H"),
"select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8
where Col1> date '"&TEXT(F1,"yyyy-mm-dd")&"' and Col3 = '"&B1&"' and Col4 = '"&D1&"'
order by Col1 desc",1)
The formula is working.
Col1 includes input dates. I retrieve only values that are after a date listed in F1.
Col3 and Col3 include some properties which are selected in cells B1 and D1, accordingly.
Col5 includes strings (client names). client name can repeat on several rows.
I'd like to retrieve just the most recent one. Any ideas on how to do it?
And, to add more fun into the question, would it be the same idea to retrieve the oldest row per client?
Here's a link to demo sheet, details in the "unique query" tab.
Another challenge can be to retrieve X number of row per client, and not just the most recent one.
try:
=SORTN(QUERY(IMPORTRANGE("1LoHg53hzQvYtOLTcDwxLY8OrKVN4F7usX8YI41BtdWg", "Activity list!A:E"),
"where Col1 > date '"&TEXT(I2, "yyyy-mm-dd")&"'
and Col2 = '"&I3&"'
order by Col1 desc", 1), 99^99, 2, 4, 1)
SORTN explained:
99^99 all rows - no limits
2 means "merge mode"
4 collapse 4th column into unique values
1 return 4th column ascending - 0 for descending
Solution
I am basing myself on a SQL expression that would achieve this result but unfortunately Google Sheets QUERY language is not as expressive. That's why the resulting formula looks a bit confusing.
=query(
IMPORTRANGE("https://docs.google.com/spreadsheets/d/1LoHg53hzQvYtOLTcDwxLY8OrKVN4F7usX8YI41BtdWg/edit","Activity list!A:E"),
"select Col1, Col2, Col3, Col4, Col5
where Col1= date'"&
JOIN("' or Col1 = date '",
ARRAYFORMULA(TEXT(ARRAY_CONSTRAIN(
query(query(
"THE IMPORTED RANGE",
"select Col1,Col2,Col3,Col4,Col5
where Col1> date '"&TEXT(I2,"yyyy-mm-dd")&"' and Col2 = '"&I3&"'
order by Col1 desc",1),
"select MAX(Col1), Col4
group by Col4
order by MAX(Col1) desc
label MAX(Col1) ''", 0),
1000, 1),
"yyyy-MM-DD")
))&"'",1)
Queries specification starting from the inner one:
Filter the data with your criteria.
Get the most recent submission grouping by Client.
Join the results with the whole dataset to fetch the other column values.
Use the ARRAY_CONSTRAIN formula to retrieve the columns with the dates.
The same approach goes for the oldest submission changing MAX for MIN aggregate function.
Note: This is not suited for daily multiple submissions.
I think the easiest way to do this is using a Vlookup into a query(). unfortunately, it involves using the IMPORTRANGE() twice, but I still think it's more efficient than some other possible methods. You'll find it in A2 of the MK.Help tab on your sample sheet.
=ARRAYFORMULA(IFERROR(VLOOKUP(UNIQUE(query(IMPORTRANGE("1LoHg53hzQvYtOLTcDwxLY8OrKVN4F7usX8YI41BtdWg","Activity list!A:E"), "select Col4 where Col1> date '"&TEXT(I2,"yyyy-mm-dd")&"' and Col2 = '"&I3&"'
order by Col1 desc",1)),query(IMPORTRANGE("1LoHg53hzQvYtOLTcDwxLY8OrKVN4F7usX8YI41BtdWg","Activity list!A:E"), "select Col4,Col1,Col2,Col3,Col5 where Col1> date '"&TEXT(I2,"yyyy-mm-dd")&"' and Col2 = '"&I3&"'
order by Col1 desc",1),{2,3,4,1,5},0)))

pl-sql include column names in query

A weird request maybe but. My boss wants me to create an admin version of a page we have that displays data from an oracle query in a table.
The admin page, instead of displaying the data (query returns 1 row), needs to return the table name and column name
Ex: Instead of:
Name Initial
==================
Bob A
I want:
Name Initial
============================
Users.FirstName Users.MiddleInitial
I realize I can do this in code but would rather just modify the query to return the data I want so I can leave the report generation code mostly alone.
I don't want to do it in a stored procedure.
So when I spit out the data in the report using something like:
blah blah = MyDataRow("FirstName")
I can leave that as is but instead of it displaying "BOB" it would display "Users.FirstName"
And I want to do the query using select * if possible instead of listing all the columns
So for each of the columns I am querying in the * , I want to get (instead of the column value) the tablename.ColumnName or tablename|columnName
hope you are following- I am confusing myself...
pseudo:
select tablename + '.' + Columnname as WhateverTheColumnNameIs
from Table1
left join Table2 on whatever...
Join Table_Names on blah blah
Whew- after writing all this I think I will just do it on the code side.
But if you are up for it maybe a fun challenge
Oracle does not provide an authentic way(there is no pseudocolumn) to get the column name of a table as a result of a query against that table. But you might consider these two approaches:
Extract column name from an xmltype, formed by passing cursor expression(your query) in the xmltable() function:
-- your table
with t1(first_name, middle_name) as(
select 1,2 from dual
), -- your query
t2 as(
select * -- col1 as "t1.col1"
--, col2 as "t1.col2"
--, col3 as "t1.col3"
from hr.t1
)
select *
from ( select q.object_value.getrootelement() as col_name
, rownum as rn
from xmltable('//*'
passing xmltype(cursor(select * from t2 where rownum = 1))
) q
where q.object_value.getrootelement() not in ('ROWSET', 'ROW')
)
pivot(
max(col_name) for rn in (1 as "name", 2 as "initial")
)
Result:
name initial
--------------- ---------------
FIRST_NAME MIDDLE_NAME
Note: In order for column names to be prefixed with table name, you need to list them
explicitly in the select list of a query and supply an alias, manually.
PL/SQL approach. Starting from Oracle 11g you could use dbms_sql() package and describe_columns() procedure specifically to get the name of columns in the cursor(your select).
This might be what you are looking for, try selecting from system views USER_TAB_COLS or ALL_TAB_COLS.

Resources