I have following task for my Google spreadsheet: JOIN strings in all cells that are to the right of certain id.
To phrase it differently: SELECT A, JOIN(',', B) GROUP BY A, WHERE A = myid; if JOIN was an aggregation function.
Or in other words: =JOIN(',', VLOOKUP(A:B, myid, 0)) if VLOOKUP could return all occurences, not just first one.
One picture better than of 1000 words:
Is this possible with Google Spreadsheets?
I believe you could use the FILTER function instead of VLOOKUP to filter a range based on an ID and then JOIN the returned range.
The documentation for FILTER is here: https://support.google.com/docs/answer/3093197
For example:
You put =UNIQUE($A:$A) in D2 to get all the IDs.
Then in E2 you put =IF($D2="", "", JOIN(",", FILTER($B:$B, $A:$A=$D2))) and then copy it down.
try:
=ARRAYFORMULA(SUBSTITUTE(REGEXREPLACE(TRIM(SPLIT(TRANSPOSE(QUERY(QUERY(
IF(B2:B<>"", {A2:A&"×", B2:B&","}, ),
"select max(Col2) where Col2 !='' group by Col2 pivot Col1")
,,999^99)), "×")), ",$", ), ", ", ","))
Related
I have strings like below in my table
2001,2452,2452,2421,2421,2495
2001,2483,2421,2421,2482
2001,2420,2421,2421,2425
2001,2420,2421,2421,2422
2001,2452,2452,2421,2421,2464
I want to remove the repeated numbers like 2452 and 2421 and show them only once in the data like
2001,2452,2421,2495
2001,2483,2421,2482
2001,2420,2421,2425
2001,2420,2421,2422
2001,2452,2421,2464
Has anyone done something like this? please let me know how to solve this
Thanks!
In Oracle SQL, You can use the hierarchy query and listagg as follows:
select str, listagg(str_distinct, ',') within group (order by 1) as distinct_str from
(select distinct str, regexp_substr(str,'[^,]+',1,column_value) str_distinct from cte
cross join table(
cast(multiset(
select level lvl
from dual
connect by level <= regexp_count(str, '[^,]+'))
as sys.odcivarchar2list)
) lvls)
group by str;
db<>fiddle for one of the input string.
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)))
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", "♦")})
Is it possible to create a custom ORDER BY function in a google sheets QUERY?
In this scenario, I don't want the results to be alphabetical nor numeric, but rather a custom order based on a predefined category list that is actually in a separate tab from both the QUERY range as well as the formula itself.
I'm not really sure where to begin: Is this even possible? Should the category list be moved to one of the other sheets? Does the category list need to be written into the formula?
Just looking for guidance at this point.
My Sheet
=ARRAYFORMULA(ARRAY_CONSTRAIN(QUERY({QUERY({Estimate!A2:H},
"where Col1 is not null"), IFERROR(VLOOKUP(QUERY({Estimate!A2:A},
"where Col1 is not null", 0),
IF(LEN(A5:A), {A5:A, ROW(A5:A)}, ), 2, 0))},
"where Col1 is not null
order by Col9", 0), 999^99, 8))
I would like to map a string column to a category based on a regular expression match.
Is it possible to use another bigquery table containing the regular expressions and corresponding category for this? This would make it easier for me to update only a table when adding new categories/updating the regex, instead of having to update all queries that would use this lookup.
Query:
CASE
-- Use the entries from another table here
WHEN REGEXP_MATCH(string_to_check, cat1regex) THEN cat1
WHEN REGEXP_MATCH(string_to_check, cat2regex) THEN cat2
etc.
END
Mapping table:
Regex category
pagex|pagey xy
pagez|page1 z1
It's also possible there is another simple way to do something similar that I'm not thinking of, answers pointing those out are welcome too.
Any help would be appreciated.
Below is for BigQuery Standard SQL
#standardSQL
SELECT
string_to_check,
MAX(IF(REGEXP_CONTAINS(string_to_check, reg), category, NULL)) AS category
FROM yourTable
CROSS JOIN mappingTable
GROUP BY string_to_check
You can test / play with it using below dummy date from your question
#standardSQL
WITH `mappingTable` AS (
SELECT r'pagex|pagey' AS reg, 'xy' AS category UNION ALL
SELECT r'pagez|page1', 'z1'
),
`yourTable` AS (
SELECT string_to_check
FROM UNNEST(["pagex.com", "pagez#example.org", "page.example.net"]) AS string_to_check
)
SELECT
string_to_check,
MAX(IF(REGEXP_CONTAINS(string_to_check, reg), category, NULL)) AS category
FROM yourTable
CROSS JOIN mappingTable
GROUP BY string_to_check