I want to use the solution explained here:
How to calculate an average profit per day with Google Sheets
But in just a single cell instead of having to use 4 different cells for the calculation.
I came up with this formula but it seems to me too complicated and inefficient:
=AVERAGEIF((arrayformula(if((arrayformula(if(query({A2:A, arrayformula(int(A2:A))}, "select max(Col1) where Col1 is not null group by Col2 label max(Col1) ''", 0)<>"", vlookup(query({A2:A, arrayformula(int(A2:A))}, "select max(Col1) where Col1 is not null group by Col2 label max(Col1) ''", 0), {A2:A,C2:C}, 2, false), "")))<>"", (arrayformula(if(query({A2:A, arrayformula(int(A2:A))}, "select max(Col1) where Col1 is not null group by Col2 label max(Col1) ''", 0)<>"", vlookup(query({A2:A, arrayformula(int(A2:A))}, "select max(Col1) where Col1 is not null group by Col2 label max(Col1) ''", 0), {A2:A,C2:C}, 2, false), "")))-QUERY((arrayformula(if(query({A2:A, arrayformula(int(A2:A))}, "select max(Col1) where Col1 is not null group by Col2 label max(Col1) ''", 0)<>"", vlookup(query({A2:A, arrayformula(int(A2:A))}, "select max(Col1) where Col1 is not null group by Col2 label max(Col1) ''", 0), {A2:A,C2:C}, 2, false), ""))), "SELECT * offset 1", 0), "")*-1)),">0")
Dummy file is here, formula is on D4:
https://docs.google.com/spreadsheets/d/1ExXtmQ8nyuV1o_UtabVJ-TifIbORItFMWjtN6ZlruWc/edit?usp=sharing
Any suggestions?
try:
=INDEX(AVERAGE(QUERY(IFNA(QUERY(
INDEX(SORTN(SORT({INT(A3:A), C3:C}, ROW(A3:A), ), 9^9, 2, 1, 1),,2), "offset 1", )-
INDEX(SORTN(SORT({INT(A3:A), C3:C}, ROW(A3:A), ), 9^9, 2, 1, 1),,2)), "offset 1", )))
Related
I'm using IMPORTRANGE and in one of the columns there are duplicates. I only want to show the row from the duplicates that has the lowest value based on another column.
My current query:
=QUERY(IMPORTRANGE("google.com/spreadsheets/d/x/edit","sheet name"),"Select Col1,Col4,Col7,Col5,Col15 where Col5 contains 'some text' and Col15 contains 'some other text' and Col4 is not null order by Col4 asc")
Based on the earlier illustration, Col4 is A and Col7 is B.
I would greatly appreciate if you could tell me how to edit the existing query.
try:
=SORTN(SORT(QUERY(
IMPORTRANGE("google.com/spreadsheets/d/x/edit", "sheet name"),
"select Col1,Col4,Col7,Col5,Col15
where Col5 contains 'some text'
and Col15 contains 'some other text'
and Col4 is not null
order by Col4 asc", ), 2, 1), 9^9, 2, 1, 1)
update:
=SORTN(SORT(QUERY(IMPORTRANGE("id","(2) NL_ranking_file.csv!A2:O250"),
"select Col1,Col4,Col7,Col5,Col15
where Col5 contains ''
and Col15 contains 'Places'
and Col4 is not null
order by Col4 asc", ), 3, 1), 9^9, 2, 2, 1)
I need to get data from :
Google Sheet #A1
to
Google Sheet #A2
(Only Gadget product from Sheet A1 and empty strings for prices)
Please advise me the formula for obtaining information.
Thank you
PRADIT SK
One formula in one cell:
={
QUERY(
IMPORTRANGE(
"https://docs.google.com/spreadsheets/d/1-4k8H08Bc5rI5WFXNHcpmWL-Sjzis9gV3-0N2imAAww",
"'Sheet-1'!A:E"
),
"SELECT Col1, Col2, Col3
WHERE Col1 IS NOT NULL
AND Col3 = 'Gadget'",
1
),
{
"PRICE";
TRANSPOSE(
SPLIT(
REPT(
",",
QUERY(
IMPORTRANGE(
"https://docs.google.com/spreadsheets/d/1-4k8H08Bc5rI5WFXNHcpmWL-Sjzis9gV3-0N2imAAww",
"'Sheet-1'!A:E"
),
"SELECT COUNT(Col1)
WHERE Col1 IS NOT NULL
AND Col3 = 'Gadget'
GROUP BY Col3
LABEL COUNT(Col1) ''",
1
) - 1
),
",",
1,
0
)
)
},
QUERY(
IMPORTRANGE(
"https://docs.google.com/spreadsheets/d/1-4k8H08Bc5rI5WFXNHcpmWL-Sjzis9gV3-0N2imAAww",
"'Sheet-1'!A:E"
),
"SELECT Col5
WHERE Col1 IS NOT NULL
AND Col3 = 'Gadget'",
1
)
}
I want to combine the Two different tables result in oracle.
conditions:
From both the tables ID ,STAMp columns are common columns.
Remaining columns are different.
Table1;
Element_ID STAMP Col1 Col2
1 22/03/2014 85 100
2 22/03/2014 95 105
Table2.
Element_ID STAMP Col3 Col4
5 22/03/2014 100 110
6 22/04/2014 200 210
Result:
Element_ID STAMP Col1 Col3
1 22/03/2014 85 null
5 22/03/2014 null 100
Query:
Select Element_ID, Stamp, Col1 from Table1 where element_ID in (1, 5)
Select Element_ID, STAMP, Col3 from Table2 where Element_ID in (1,5)
How to combine the above two queries results, and results should come as above format.
You can try as this,
Select Element_ID, Stamp, Col1, NULL Col3 from Table1 where element_ID in (1,5)
union
Select Element_ID, STAMP, NULL Col1, Col3 from Table2 where Element_ID in (1,5)
Select T1.Element_ID,T1.Stamp, T1.Col1, T2.Col3
from Table1 T1, Table2 T2
where T1.Element_ID in (1, 5)
This is one interesting use case for a RIGHT JOIN on some minimum set -- both to restrict the result to records 1 and 5 and to force the presence of those two records:
SELECT "Element_ID", "STAMP", "Col1", "Col3"
FROM Table1
FULL OUTER JOIN Table2
USING("Element_ID", "STAMP")
RIGHT JOIN
(
SELECT 1 "Element_ID" FROM DUAL
UNION ALL SELECT 5 FROM DUAL
) MinimumSet
USING("Element_ID")
See http://sqlfiddle.com/#!4/725d9/18 for a live example
I have table structure like:
CREATE TABLE new_test
( col1 NUMBER(2) NOT NULL,
col2 VARCHAR2(50) NOT NULL,
col3 VARCHAR2(50) NOT NULL,
col4 VARCHAR2(50) NOT NULL
);
It has data:
col1 col2 col3 col4
0 A B X
1 A B Y
2 A B Z
1 C D L
3 C D M
I need to find value of col4 which has maximum value for combination of col2 and col3. e.g. my result should be:
col4
Z
M
I tried to use oracle analytic function:
SELECT col4, MAX(col1) OVER (PARTITION BY col2, col3) FROM (
SELECT col2, col3, col4, col1
FROM new_test);
But it is not working as expected. Can you please help me to resolve this issue?
Update:
I could make it work using:
SELECT a.col4
FROM new_test a,
(SELECT col2,
col3,
col4,
MAX(col1) OVER (PARTITION BY col2, col3) AS col1
FROM new_test
) b
WHERE a.col2 = b.col2
AND a.col3 = b.col3
AND a.col4 = b.col4
AND a.col1 = b.col1;
Is there any better way than this?
If you expect that result:
col4
Z
M
You should write:
SELECT MAX(col4) AS col4 FROM new_test GROUP BY col2,col3
This will return the maximum value found in column col4 for each pair of col2,col3
If you need that:
COL2 COL3 COL4
A B Z
C D M
Write:
SELECT col2,col3,MAX(col4) AS col4 FROM new_test GROUP BY col2,col3
Finally, if you need:
COL1 COL2 COL3 COL4
2 A B Z
3 C D M
There are many variations. Like this one:
SELECT col1,col2,col3,col4
FROM new_test
NATURAL JOIN (SELECT col2,col3,MAX(col4) AS col4
FROM new_test GROUP BY col2,col3)
I need to transform the rows into columns for the similar ID's in oracle
e.g.
The following is the result I will get if i query my database
Col1 Col2 Col3
---- ---- ----
1 ABC Yes
1 XYZ NO
2 ABC NO
I need to transform this into
Col1 Col2 Col3 Col4 Col5
---- ---- ---- ---- ----
1 ABC Yes XYZ No
2 ABC NO NULL NULL
Someone please help me in solving this issue
Thanks,
Siv
Based on AskTom:
select Col1,
max( decode( rn, 1, Col2 ) ) Col_1,
max( decode( rn, 1, Col3 ) ) Col_2,
max( decode( rn, 2, Col2 ) ) Col_3,
max( decode( rn, 2, Col3 ) ) Col_4
from (
select Col1,
Col2,
Col3,
row_number() over (partition by Col1 order by Col2 desc nulls last) rn
from MyTable
)
group by Col1;
I don't have access to an Oracle db to test it but I think that will work. If there could be more than two records per ID then you could just add more rows to the select cause with the corresponding row number.
One solution is to use the 10g MODEL clause:
SQL> select col1
2 , col2
3 , col3
4 , col4
5 , col5
6 from t23
7 model
8 return updated rows
9 partition by ( col1 )
10 dimension by ( row_number() over ( partition by col1
11 order by col2 desc nulls last) rnk
12 )
13 measures (col2, col3, lpad(' ',4) col4, lpad(' ',4) col5)
14 rules upsert
15 (
16 col2 [0] = col2 [1]
17 , col3 [0] = col3 [1]
18 , col4 [0] = col2 [2]
19 , col5 [0] = col3 [2]
20 )
21 /
COL1 COL2 COL3 COL4 COL5
---------- ---- ---- ---- ----
1 ABC Yes ABC NO
2 XYZ NO
SQL>
It is an unfortunate truth about such solutions that we need to specify the number of columns in the query. That is, in regular SQL there is no mechanism for determining that the table contains three rows where COL1 = 1 so we need seven columns, which is not unreasonable. For situations in which the number of pivot values is unknown at the time of coding there is always dynamic sql.