Google sheet query 2 columns as search key and search - google-sheets-formula

I met some problem with google sheet function.
I have 2 tables. I want to search table1 Date+User as key value in table2.
example:
Date User Unit
2022/05/30 A 109
2022/05/30 B 119
2022/05/30 C 119
2022/05/29 D 109
2022/05/29 E 114
Date User Amount
2022/05/30 A 1
2022/05/30 B 2
2022/05/30 C 3
2022/05/30 D 41
2022/05/30 E 5
2022/05/29 D 6
2022/05/29 E 7
2022/05/29 F 81
2022/05/29 G 9
2022/05/29 A 101
2022/05/29 B 11
2022/05/29 C 121
2022/05/29 D 13
after query I hope the table looks like
Hope Result
Date User Unit Amount
2022/05/30 A 109 1
2022/05/30 B 119 2
2022/05/30 C 119 3
2022/05/29 D 109 6
2022/05/29 E 114 7
This is a sample google sheet
https://docs.google.com/spreadsheets/d/1oxhWMVPt-GziG10agob-xbiNYfKrZVFK9ro0Pj7tn6Y/edit#gid=0
Can I ask for help ?
Many Thanks

Two options. The first pulls all matching combinations of DATE and USER
=ARRAYFORMULA(
QUERY(
{E2:G,
IF(ISBLANK(E2:E),,
IFERROR(
VLOOKUP(
E2:E&"|"&F2:F,
{A2:A&"|"&B2:B,C2:C},
2,FALSE)))},
"select Col1, Col2, Col4, Col3
where Col4 is not null
label
Col1 'Date',
Col2 'User',
Col3 'Amount',
Col4 'Unit'"))
which returns
Date
User
Unit
Amount
2022/05/30
A
109
1
2022/05/30
B
119
2
2022/05/30
C
119
3
2022/05/29
D
109
6
2022/05/29
E
114
7
2022/05/29
D
109
13
The second matches your output exactly, but does omit that second D value for the 29th (13)
=ARRAYFORMULA(
QUERY(
{IFERROR(
VLOOKUP(
UNIQUE(E2:E&"|"&F2:F),
{E2:E&"|"&F2:F,E2:G},
{2,3,4},FALSE)),
IFERROR(
VLOOKUP(
UNIQUE(E2:E&"|"&F2:F),
{A2:A&"|"&B2:B,C2:C},
2,FALSE))},
"where Col4 is not null
format Col1 'yyyy/mm/dd'"))
Both have been added to your sheet. If either of these work out for you, I can break it down.

Related

Creating subset of a data

I have a column called Project_Id which lists the names of many different projects, say Project A, Project B and so on. A second column lists the sales for each project.
A third column shows time series information. For example:
Project_ID Sales Time Series Information
A 10 1
A 25 2
A 31 3
A 59 4
B 22 1
B 38 2
B 76 3
C 82 1
C 23 2
C 83 3
C 12 4
C 90 5
D 14 1
D 62 2
From this dataset, I need to choose (and thus create a new data set) only those projects which have at least 4 time series points, say, to get the new dataset (How do I get this by using an R code, is my question):
Project_ID Sales Time Series Information
A 10 1
A 25 2
A 31 3
A 59 4
C 82 1
C 23 2
C 83 3
C 12 4
C 90 5
Could someone please help?
Thanks a lot!
I tried to do some filtering with R but had little success.

Hierarchical Update in Hive

I got a hive table as follows:
Table A
docid corr_docid header
100 a
101 100 b
102 c
105 101 d
106 102 e
107 106 f
108 107 g
109 h
Is it possible to create another table.
Here corr_docid 107 corrects the document with docid 107.
Table B as follows:
Table A
docid corr_docid header newdocid
100 a 105
101 100 b 105
102 c 108
105 101 d 105
106 102 e 108
107 106 f 108
108 107 g 108
109 h 109
Is this possible in hive.
You can try this native SQL to get desired result, This will work only if you know the hierarchy depth/level, is 4 here.
`select a.docid,
a.corr_docid,
case when b.docid is null then a.docid
when c.docid is null then b.docid
when d.docid is null then c.docid
else d.docid
end newdocid
from Table_A a left join Table_A b on a.docid = b.corr_docid
left join Table_A c on b.docid = c.corr_docid
left join Table_A d on c.docid = d.corr_docid ;`

Creating multiple extract from single mapping in Informatica

I have below source table.
Col1 Col2 Col 3 Col4 col5 col6 col7 col8
A 2 1 2 3 4 5 AAA
B 3 1 1 8 5 6 AAA
C 4 1 2 9 6 7 CC
D 5 2 3 10 7 8 CC
E 2 2 4 11 8 9 CC
F 3 3 5 12 9 10 BB
G 4 3 6 13 10 11 BB
H 5 3 7 14 11 12 BB
I 6 3 8 15 12 13 BB
I want to create a single mapping ( 1 source and 1 target stucture) which should create three extract from above source as below. All differnt extract will have different number of columns based on saome speific id.
Extract 1
Col1 Col2 Col 3 Col4 col8
A 2 1 2 AAA
B 3 1 1 AAA
Extract 2
Col1 Col2 Col 3 Col4 col5 col6 col7 col8
C 4 1 2 9 6 7 CC
D 5 2 3 10 7 8 CC
E 2 2 4 11 8 9 CC
Extract 3
Col1 Col2 col7 col8
F 3 10 BB
G 4 11 BB
H 5 12 BB
I 6 13 BB
I dont want to create three differnt target structure.
Please let us know if any one have any idea on that .
You can split the flow to three pipelines with three Target Definitions that all will point to the same Target Table on session level. This will be clear and effectively all will end up in same target structure.
Otherwise, if for some reason you's like to avoid having three target instances, you can use the Union Transformation that will span the three pipelines and send all data to one target instance.

How to order by a table based on the other tables column

i have 2 tables
Table 1
col1 col2
---- ----
BLANK A
D A
V A
BLANK B
D B
V B
Table 2
col1 col2 sex age
---- ---- ---- ----
A as M 45
A sa F 32
A asd F 45
B as M 45
B sa F 32
B asd F 45
my output should be in a order like for each value in col1 of Table 1 should be repeated with the Table2 values and that to in the order and in the second table it should be ordered by Sex and for Female records alone we need to order them by age older first. finally the output table should look like below.
COL1 COL2 COL3 SEX AGE
---- ----- ---- --- ---
BLANK A as M 45
BLANK A asd F 45
BLANK A sa F 32
D A as M 45
D A asd F 45
D A sa F 32
V A as M 45
V A asd F 45
V A sa F 32
BLANK B as M 45
BLANK B asd F 45
BLANK B sa F 32
D B as M 45
D B asd F 45
D B sa F 32
V B as M 45
V B asd F 45
V B sa F 32
If you just want the rows repeating for each value without any join criteria, something like this should do (although I don't have Sybase to test on, it's fairly straight forward SQL);
SELECT t1.col1, t1.col2, t2.col2 AS col3, sex, age
FROM Table1 t1, Table2 t2
ORDER BY t1.col1, sex DESC, age DESC
An SQL Server SQLfiddle as a sample.
EDIT: After your edited question, I assume you want to pair t1.col2 with the corresponding t2.col1, then this ordering should be the correct one;
SELECT t1.col1, t1.col2, t2.col2 AS col3, sex, age
FROM Table1 t1
JOIN Table2 t2
ON t1.col2 = t2.col1
ORDER BY t1.col2, t1.col1, sex DESC, age DESC
Another SQLfiddle.

Getting the first occurrence of group of rows

Am having a case where I have transaction and this transaction consists of several steps. I want to return the first occurrence of this transaction, for example:
Trn Loc step
111 0 1
111 0 2
111 0 3
222 3 1
222 3 2
333 5 1
333 5 2
333 5 3
and i want to get this result :
tran loc
111 0
222 3
333 5
I think it is supposed to be done by partition function but I don't know how...any help please?
select t.trn, t.loc
from (select trn, loc, ROW_NUMBER() OVER (PARTITION BY trn, loc ORDER BY trn, loc) as rnum
from table ) t
where t.rnum = 1
Or you can use RANK() function instead of ROW_NUMBER(), rest of syntax is same.
http://www.techonthenet.com/oracle/functions/rank.php

Resources