https://docs.google.com/spreadsheets/d/1AQUNPb4d3EqeSZb9SwfzemR7MfSIkMFkDAZ8rPLA3rc/edit?usp=sharing
I have 3 columns :
City
Date start
End date
I want 3 table :
Pivot Table city with people which enter during the year (Done)
=query(QUERY({$A$2:$C$10};
"select Col1, count(Col1)
where year(Col2)=2018 or year(Col2)=2019 or year(Col2)=2020
group by Col1
pivot year(Col2)");
"select * order by Col4 desc, Col3 desc, Col2 desc label Col1 'Start'";1)
Pivot Table city with people which left during the year (Done)
=query(QUERY({$A$2:$C$10};
"select Col1, count(Col1)
where (year(Col3)=2018 or year(Col3)=2019 or year(Col3)=2020)
group by Col1
pivot year(Col3)");
"select * order by Col4 desc, Col3 desc, Col2 desc label Col1 'End'";1)
- Pivot Table city with people which stay during the year (Fail)
=query(QUERY({$A$2:$C$10};
"select Col1, count(Col1)
where
(2018>=YEAR(Col2) and 2018<=YEAR(Col3) or
(2019>=YEAR(Col2) and 2019<=YEAR(Col3) or
(2020>=YEAR(Col2) and 2020<=YEAR(Col3)
group by Col1
pivot year(Col2)");
"select * order by Col4 desc, Col3 desc, Col2 desc label Col1 'Between'";1)
For the last one, i am getting trouble.
I guess my Where condition is not adapted and my pivot not working too.
I know pivot year(Col2) can't work for the last one, because if a row got a date start 2015 and 2020 end start, i want it to be counted, but my pivot won't show up 2018 2019 2020.
Any idea ?
Thanks for your time
use:
=ARRAYFORMULA(QUERY(QUERY(UNIQUE(SPLIT(FLATTEN(IF(DAYS(C2:C10; B2:B10)>=
SEQUENCE(1; 5000; ); ROW(A2:A10)&"×"&A2:A10&"×"&TEXT(B2:B10+SEQUENCE(1; 5000; );
"yyyy-1-1"); )); "×"));
"select Col2,count(Col2)
where year(Col3) matches '2018|2019|2020'
group by Col2
pivot year(Col3)
label Col2'Between'");
"order by Col4 desc, Col3 desc, Col2 desc"))
update:
=ARRAYFORMULA(QUERY(QUERY(SPLIT(FLATTEN(IF(DATEDIF(B2:B; C2:C; "Y")>=
SEQUENCE(1; MAX(DATEDIF(B2:B; C2:C; "Y")); ); ROW(A2:A)&"×"&A2:A&"×"&
YEAR(B2:B)+SEQUENCE(1; MAX(DATEDIF(B2:B; C2:C; "Y")); )&"-1-1"; )); "×");
"select Col2,count(Col2)
where year(Col3) matches '2018|2019|2020'
group by Col2
pivot year(Col3)
label Col2'Between'");
"order by Col4 desc, Col3 desc, Col2 desc"))
I have a list of transactions in Transactions tab and in Summary I would like to summarize by tickers the performance. I am using query for grouping the data and using aggregate functions to calculate %-Win, %-Lost (see the link at the bottom with the sample spreadsheet):
Transaction tab:
=query({Transactions!B:B,Transactions!C:F},
"select Col1, count(Col2),sum(Col4),
(count(Col2)/(count(Col2)+count(Col3))), count(Col3),
sum(Col5),
(count(Col3)/(count(Col3)+count(Col2))) where Col1 is not NULL
and
(Col2 is not NULL or Col3 is not Null)
group by Col1
label count(Col2) 'Win', sum(Col4) '$-Win',
(count(Col2)/(count(Col2)+count(Col3))) '%-Win',
count(Col3) 'Lost', sum(Col5) '$-Lost',
(count(Col3)/(count(Col3)+count(Col2))) '%-Lost'",1)
Sample of Summary tab:
but I was not able to obtain from the query by ticker: Total Transactions, Net Gains, Exp. Value(Expected Value), so I did use Arrayformula, and it works, the problem is that I am not able to sort the result by expected value nor Net Gain (FUBO should be first). I was able to calculate percentage using a combination of aggregated functions, but not for the above additional calculations directly in the query.
I tried to use query clause order by: sum(Col3)+sum(Col5) (Net gains) but it doesn't work, it only returns a value when there are Win and Lost transactions.
Using Data->Sort Range doesn't provide the expected result either. Because there are different sources of data: the query and the result of Arrayformula.
I guess I would need to obtain all required calculated fields directly from the query and then to order by, or to find a way to sort the result combining the query and Arrayformula results. The clause order by works well for aggregated functions that are present in the select elements, but not when the sorting should happen based on a formula based on calculated columns.
Here you can find a sample file from my real situation:
https://docs.google.com/spreadsheets/d/1xrDSWGJVIsWD6fvAOdMOZkw2rEY9lGPZRb_Ww_nC7YQ/edit?usp=sharing
Note: A possible solution would be to combine all the results into one sort statement, but I am not able to make it work
=sort({
query({Transactions!B2:B,Transactions!C2:F}, "select Col1, count(Col2),sum(Col4), (count(Col2)/(count(Col2)+count(Col3))), count(Col3), sum(Col5), (count(Col3)/(count(Col3)+count(Col2))) where Col1 is not NULL and (Col2 is not NULL or Col3 is not Null) group by Col1 label count(Col2) '', sum(Col4) '', (count(Col2)/(count(Col2)+count(Col3))) '', count(Col3) '', sum(Col5) '', (count(Col3)/(count(Col3)+count(Col2))) ''",0),
ARRAYFORMULA(if(not(ISBLANK(A2:A)), B2:B+E2:E,)),
ARRAYFORMULA(if(not(ISBLANK(A2:A)), C2:C+F2:F,)),
ARRAYFORMULA(if(not(ISBLANK(A2:A)), (C2:C)*(D2:D) + (F2:F)*(G2:G),))
},10, FALSE)
In the same way avoiding using Arrayformula using two query statements, doesn't work:
=sort({
query({Transactions!B2:B,Transactions!C2:F}, "select Col1, count(Col2),sum(Col4), (count(Col2)/(count(Col2)+count(Col3))), count(Col3), sum(Col5), (count(Col3)/(count(Col3)+count(Col2))) where Col1 is not NULL and (Col2 is not NULL or Col3 is not Null) group by Col1 label count(Col2) '', sum(Col4) '', (count(Col2)/(count(Col2)+count(Col3))) '', count(Col3) '', sum(Col5) '', (count(Col3)/(count(Col3)+count(Col2))) ''",0),
query(query({Transactions!B2:B,Transactions!C2:F}, "select Col1, count(Col2),sum(Col4), (count(Col2)/(count(Col2)+count(Col3))), count(Col3), sum(Col5), (count(Col3)/(count(Col3)+count(Col2))) where Col1 is not NULL and (Col2 is not NULL or Col3 is not Null) group by Col1 label count(Col2) '', sum(Col4) '', (count(Col2)/(count(Col2)+count(Col3))) '', count(Col3) '', sum(Col5) '', (count(Col3)/(count(Col3)+count(Col2))) ''",0),"select Col2+Col5 label Col2+Col5 ''",0),
query(query({Transactions!B2:B,Transactions!C2:F}, "select Col1, count(Col2),sum(Col4), (count(Col2)/(count(Col2)+count(Col3))), count(Col3), sum(Col5), (count(Col3)/(count(Col3)+count(Col2))) where Col1 is not NULL and (Col2 is not NULL or Col3 is not Null) group by Col1 label count(Col2) '', sum(Col4) '', (count(Col2)/(count(Col2)+count(Col3))) '', count(Col3) '', sum(Col5) '', (count(Col3)/(count(Col3)+count(Col2))) ''",0), "select Col3+Col6 label Col3+Col6 ''",0),
query(query({Transactions!B2:B,Transactions!C2:F}, "select Col1, count(Col2),sum(Col4), (count(Col2)/(count(Col2)+count(Col3))), count(Col3), sum(Col5), (count(Col3)/(count(Col3)+count(Col2))) where Col1 is not NULL and (Col2 is not NULL or Col3 is not Null) group by Col1 label count(Col2) '', sum(Col4) '', (count(Col2)/(count(Col2)+count(Col3))) '', count(Col3) '', sum(Col5) '', (count(Col3)/(count(Col3)+count(Col2))) ''",0), "select Col3*Col4+Col6*Col7 label Col3*Col4+Col6*Col7 ''",0)
},10, FALSE)
Doesn't give all the result values for Net Gain and Exp. Value
As you can see it only provides Net Gains and Exp. Value where are Win and Lost values on the same row.
You should fill the blanks with 0.
=SORT(QUERY(query(ArrayFormula({Transactions!B:B,
IF(Transactions!C:F="",0, Transactions!C:F)}),
"select Col1, sum(Col2),sum(Col4),
(sum(Col2)/(sum(Col2)+sum(Col3))),
sum(Col3), sum(Col5), (sum(Col3)/(sum(Col3)+sum(Col2)))
where Col1 is not NULL and NOT (Col2 = 0 and Col3 = 0) group by Col1",1),
"select Col1, Col2, Col3, Col4, Col5, Col6, Col7,Col2+Col5,
Col3+Col6,Col3*Col4+Col6*Col7
label Col2 'Win',Col3 '$-Win', Col4 '%-Win', Col5 'Lost',
Col6 '$-Lost', Col7 '%-Lost', Col2+Col5 'Total Transactions',
Col3+Col6 'Net Gains',Col3*Col4+Col6*Col7 'Exp. Value'",1),
10,FALSE)
Notes:
The condition: NOT (Col2 = 0 and Col3 = 0)ensures to exclude transactions that were not sold, i.e. Win =0 and Lost = 0
The condition: IF(Transactions!C:F="",0, Transactions!C:F)ensures empty values are replaces by 0to ensure the agregate SQL functions work as expected
Hello I have a function that is running two queries. I would like to add the letter E to the end of the first Query Results and the Letter B to the end of the query results.
My current function is
={QUERY(importRange("*****", "'Inventory'!A3:D"), "SELECT Col2 WHERE ((Col3) > 0)", 1);QUERY(importRange("******", "'Inventory'!A3:D"), "SELECT Col2 WHERE ((Col4) > 0)", 1)}
use:
=ARRAYFORMULA({QUERY(IMPORTRANGE(
"1FpeC9Nck7aGjTEoHg5R0QQBC4S5zj4Yiu_-p0AjvqXY", "'Inventory'!A3:D"),
"select Col2 where Col3 > 0", 1)&"E"; QUERY(IMPORTRANGE(
"1FpeC9Nck7aGjTEoHg5R0QQBC4S5zj4Yiu_-p0AjvqXY", "'Inventory'!A3:D"),
"select Col2 where Col4 > 0", 1)&"B"})
I have a data set (11,11,14,14,10).
My goal is to return all frequently appeared numbers. I used =mode() function.
But, it does not return 11 and 14, only returns 11.
Any ideas/thoughts on that?
With layout as shown,
=query(A2:A6,"select count(A), A group by A order by count(A) desc label count(A) 'frequency'")
should return a listing of all frequencies in descending order:
=QUERY(QUERY(A1:A, "select count(A), A
group by A
order by count(A) desc"),
"select Col2 where Col1 > 1", 0)
=TRANSPOSE(QUERY(QUERY(QUERY(TRANSPOSE(E1:1), "select *"),
"select count(Col1), Col1
group by Col1
order by count(Col1) desc"),
"select Col2 where Col1 > 1", 0))