Google Sheets nested query statements with different ranges - if-statement

I have two pivot tables with the same columns, but different filters
PT1 (P3:V) - shows all CR data
PT2 (Y3:AF) - shows only RESC CR data
I have a page that summarizes this data in a "prettier format" and have a query in B5 that will query data based on what is selected in C2. I was able to get it to query all if they select all or by specific reviewer's names (both query PT1), using the following formula:
=IF(C2="All", QUERY({'Pivot Table 5'!P3:W1001}, "SELECT P, Q, S, T, U, V, W"), QUERY({'Pivot Table 5'!P3:W1001}, "SELECT P, Q, S, T, U, V, W WHERE P = '"&$C$2&"'"))
I am trying to add to the formula so that it queries only RESC CRs if "All RESC CRs" is selected in C2 (PT2).
Here is the formula I am trying to use:
=IFS(AND(C2="All"), QUERY({'Pivot Table 5'!P3:W1001}, "SELECT P, Q, S, T, U, V, W"), AND(C2="All RESC CRs"), QUERY({'Pivot Table 5'!Y3:AY1001}, "SELECT AR, AS, AU, AV, AW, AX, AY"), AND(C2<>"All", C2<>"All RESC CRs"), Query({'Pivot Table 5'!P3:W}, "SELECT P, Q, S, T, U, V, W WHERE P = '"&$C$2&"'"))
Here is the sample sheet: https://docs.google.com/spreadsheets/d/18SC_hQbWeFlYedLN5k0fuuwqgi8fdyW1A4MCfFTpVME/edit?usp=sharing
I'm assuming it is because the query range for the second query is different? Any help is greatly appreciated!

try:
=IF(C2="All", QUERY('Pivot Table 5'!P3:W, "select P,Q,S,T,U,V,W"),
IF(C2="All RESC CRs"), QUERY('Pivot Table 5'!Y3:AY, "select AR,AS,AU,AV,AW,AX,AY"),
QUERY('Pivot Table 5'!P3:W, "select P,Q,S,T,U,V,W where P = '"&C2&"'")))
update:
=IF(C2="All", QUERY('Pivot Table 5'!P3:W, "select P,Q,S,T,U,V,W", ),
IF(C2="All RESC CRs", QUERY('Pivot Table 5'!P3:W, "select P,Q,S,T,U,V,W where R contains 'RESC'",),
QUERY('Pivot Table 5'!P3:W, "select P,Q,S,T,U,V,W where P = '"&C2&"'",)))

Related

Lookup multiple columns in Google Sheets and return a list from the matches

I think the title accurately describes what I'm trying to achieve.
https://docs.google.com/spreadsheets/d/1sRQzXXZ4a3vjAwvsH4rrWfijxV4jCl_OV3iQO00yvlQ/edit?usp=sharing
Essentially, I have a table of data for houses, the street it's on, whether it has a pool or gates etc. and I'm trying to create a lookup in Google Sheets so if someone is trying to find a house with a pool for a maximum of $800k then I can return results that match the criteria.
This is how the table data looks.
I want to be able to query the data here in columns D, E, F, G (G being a maximum value in the lookup) and return the data in columns A, B, C if everything matches.
I would enter on a different tab, the maximum budget (which would need to do a max lookup of column G, and then look for any Y/N in the other columns and return a list of all matches.
Is this possible with Google Sheets?
Thanks, for any help you can offer.
use:
=QUERY(Houses!A:I,
"select C,B,A,H
where H <= "&B3&"
and D = '"&B4&"'
and E = '"&B5&"'
and F = '"&B6&"'", 0)
update:
=IFERROR(QUERY(HousingData,
"select C,B,A,G
where G <= "&B3&
IF(B4="Y", " and D = '"&B4&"'", )&
IF(B5="Y", " and E = '"&B5&"'", )&
IF(B6="Y", " and F = '"&B6&"'", )&
IF(B7="Y", " and J = '"&B7&"'", ), 0), "No houses found.")

How to use RelBuilder.pivot() method for a query without an alias passed for Pivot expression values?

https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
Here in RelBuilder class documentation for pivot() method provided example is below.
for SQL ::
SELECT * FROM (SELECT mgr, deptno, job, sal FROM emp) PIVOT (SUM(sal) AS ss, COUNT(*) AS c FOR (job, deptno) IN (('CLERK', 10) AS c10, ('MANAGER', 20) AS m20))
RelBuilder.pivot(groupKey, aggCalls, axes, valueMap.build().entrySet())
can be used to create a RelNode object. Here we pass a Map(alias, pivot_column_values) object as "values" parameter.
final Function<RelBuilder, RelNode> f = b -> b.scan("emp")
.pivot(b.groupKey("MGR"), Arrays.asList(b.sum(b.field("SAL")).as("SS"), b.count().as("C")),
b.fields(Arrays.asList("JOB", "DEPTNO")),
ImmutableMap.<String, List<RexNode>>builder()
.put("C10", Arrays.asList(b.literal("CLERK"), b.literal(10)))
.put("M20", Arrays.asList(b.literal("MANAGER"), b.literal(20))).build().entrySet())
.build();
How to pass "values" parameter when we don't have alias for pivot column values.
e.g. how to pass "values" parameter of RelBuilder.pivot() method for below query
SELECT * FROM (SELECT mgr, deptno, job, sal FROM emp) PIVOT (SUM(sal) AS ss, COUNT(*) AS c FOR (job, deptno) IN (('CLERK', 10), ('MANAGER', 20)))
Also is it compulsory to have at least one entry in values Map?

Postgres C extended data type definition

When dealing with the following problems, Postgres is a bit tricky to deal with more complex structures. I want to set up a two-dimensional array of structure, but I don't know how to make Postgres C support me to do so? Do anyone have any ideas?
Table
id contents(text) num(double)
1 I love you. {1,3,4,5,6,7,8,10}
2 why do it? {3,4,2,11,12,33,44,15}
3 stopping. {22,33,11,15,14,22,11,55}
4 try it again. {15,12,11,22,55,21,31,11}
Sort the rows of each position of the array to get the fo.lowing structure. The result of the first row below is the first position of the num field column array, and so on.the count 4 refers to returning the first n sorted.
select my_func(contents, num, 4) from table;
expected result:
result
{('stopping.', 22), ('try it again.', 15), ('why do it?', 3), ('I love you.', 1)}
{('stopping.', 33), ('try it again.', 12), ('why do it?', 4), ('I love you.', 3)}
{('stopping.', 11), ('try it again.', 11), ('I love you.', 4), ('why do it?', 2)}
......
......
Thanks in advance.
I'm not sure why you need C extended data type, but the following will give you what you want and can be implemented as plpgsql function.
WITH t1 AS (
SELECT id, contents, unnest (num) AS n FROM table
),
t2 AS (
SELECT id, contents, n,
row_number () OVER (PARTITION BY id ORDER BY id) AS o
FROM t1 ORDER BY o ASC, n DESC, id ASC
),
t3 AS (
SELECT array_agg (ROW (contents, n)) AS a, o
FROM t2 GROUP BY o ORDER BY o
)
SELECT array_agg (a ORDER BY o) FROM t3;
UPDATE: Problem of the above may be undefined order of 'unnest'.
The following gives consistent relation between index and num, but need to write the size of num array explicitly.
WITH RECURSIVE t1 (i, id, contents, num) AS (
SELECT 1, id, contents, num[1] FROM table
UNION ALL
SELECT t1.i + 1, table.id, table.contents, table.num[t1.i + 1]
FROM t1, table
WHERE t1.id = table.id AND t1.i < 8 -- put here size of array
),
t2 (i, d) AS (
SELECT i, array_agg (ROW (contents, num) ORDER BY num DESC)
FROM t1 GROUP BY i
)
SELECT array_agg (d ORDER BY i) FROM t2;

Redshift: insert column C1 of table T1 into column C2 of Table T2

I have two tables:
T1 with columns A1, A2, A3, A4,...., A20.
T2 with columns B1, B2, B3,...., B15.
The data type of all columns is varchar.
I want to copy all values of column range A1-A10 to B1-B10. How do I do so in Redshift? I tried:
insert into T2(B1,B2,...,B10) select A1 A2 A3 ... A10 from T1
but it failed. I corrected errors like missing ), (dot) in the column name.
How can I insert selected column from one table to another? Is there any other way to do that?
You need to do insert into T2 (select A1, A2 ... A10 from T1).
I tested with following queries and things worked fine for me:
create temp table T1 (a varchar(5), b varchar(5), c varchar(5), d varchar(5), e varchar(5));
insert into T1 values ('t11', 't12', 't13', 't14', 't15');
create temp table T2 (a varchar(5), b varchar(5), c varchar(5));
insert into T2 values ('t21', 't22', 't23');
insert into T2 (select a, b, c from T1);
select * from T2;
The last line correctly printed the following:
t21 t22 t23
t11 t12 t13

converting values from string to int in hive

I am creating a table in hive;
create table patients(
patient_id INT,
age_group STRING,
gender STRING,
income_range STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ';
load data local inpath '/mnt/patients.csv' into table patients;
Now when I am using the command:
hive>select * from patients limit 5;
I am getting the output:
NULL 75-84, F, 32000-47999
NULL 75-84, M, 16000-23999
NULL 85+, M, <16000
NULL 65-74, F, 32000-47999
NULL <65, M, <16000
But when I am using assigning patient_id as string its showing:
910997967, 75-84, F, 32000-47999
506013497, 75-84, M, 16000-23999
432041392, 85+, M, <16000
633048699, 65-74, F, 32000-47999
I tried to use :
hive>select CAST(patient_id AS int) from patients;
But its not changing the values to int and only showing
NULL
NULL
...
How could the values of patient_id can be converted to int values?
Thanks
As #visakh pointed out that there is a comma(,) in your 1st column: patient_id.
You need to remove this.
You may use
CAST(regexp_replace(patient_id, ',' , '') AS INT)
This is similar to
Hive function to replace comma in column value