How can I select two columns in a query and order them by a third column? - sorting

Basically, is there a way to do this?
select B,C,SUM(I) group by B,C order by L
I've added the row numbers to L, but if there's a way to get row numbers in the formula, I'd love to know. Thanks.

try perhaps:
=ARRAY_CONSTRAIN(QUERY(A:L,
"select B,C,sum(I),L
where B is not null
group by B,C,L
order by L
label sum(I)''", 0), 999^99, 3)

This formula will group by 2 columns and sort by the 3-d column.
=ARRAY_CONSTRAIN(QUERY(
FILTER({B:B,C:C,I:I,VLOOKUP(B:B&C:C,{B:B&C:C,L:L},2,)},B:B<>""),
"select Col1,Col2,sum(Col3),Col4
where Col1 is not null
group by Col1,Col2,Col4
order by Col4", 1), 999^99, 3)
ARRAY_CONSTRAIN to hide sort column
FILTER + VLOOKUP to select first entries of column 3 (L) in order to skip duplicates.

Related

Can you help me when I select Desc in dropdown list google sheet I want the value become unique number

I have a google Spreadsheet.
Column A = Unique number,
Column B = Desc
In column C, I use data validation from column B. I want when I select 1 value the result in column C should be column A. I tried include image to become more clear. Do you have any ideas?
Thank you.
use:
=IFNA(VLOOKUP(D3, {B:B, A:A}, 2, 0))
or:
=IFNA(FILTER(B:B, A:A=D3))

ArrayFormula with sum of previous rows

I have an ArrayFormula to calculate a value for each row, and for each 6th row I want it to calculate the sum of the previous 5 instead.
Example sheet: https://docs.google.com/spreadsheets/d/18g2bOOBqsUgmy3ZXINOl6hcaMXf-uYJv7PGft247FjU/edit?usp=sharing
I have tried several routes, including google script, but keep banging my head against limitations of ArrayFormula.
You need make group by rows
My E.g
Cell A2 (Name groups):
=ArrayFormula(IF(B2:B<>"",FLOOR((ROW(A2:A)-2)/5)+1,""))
Column B (Your Data)
Cell E2 (Result):
=QUERY({QUERY({A2:B},"select Col1,sum(Col2) where Col1>0 group by Col1");
QUERY({A2:B},"select Col1,Col2 where Col1>0")},
"select Col2 where Col1>0 order by Col1,Col2 label Col2 ''")
Function References
Query

Combine rows based on multiple criteria and then sum one column

Group rows together when columns A,B,C,D,F match (D can be blank and still be considered a match) and then sum the "Qty" column.
I can FILTER and I can SUM, but I can't seem to do both.
This formula will be in 'estimate-Tally'!A2
=QUERY(QUERY(A2:X6,
"select A,B,C,' ',' ',F,sum(G),H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X
where A is not null
group by A,B,C,' ',' ',F,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X", 0),
"offset 1", 0)

Alternate row with value from IN operator

Is this possible?
I have a sample query:
select vehicle_name
from vehicles
where vehicle_name in ('TOYO', 'HOND');
I may have a lot vehicle_name in the IN operator clause. The result it returns is in the screenshot given below (screenshot 1).
What I want is in the second screenshot (screenshot 2) where first row should be HOND and second row should be TOYO. (based on alphabetical order) Third row should be HOND and fourth row should be TOYO. so on and so forth. In other words, two HOND or two TOYO should not come one after the other until the end of the result where no alternate vehicle_name is found.
Thanks,
You could use the analytic function ROW_NUMBER() to generate sequence numbers separately for each vehicle_name and use that for ordering. You don't need to add the function to SELECT - you can use it directly in ORDER BY.
SELECT vehicle_name
FROM vehicles
WHERE vehicle_name in ('HOND', 'TOYO')
ORDER BY row_number() over (partition by vehicle_name order by null), vehicle_name
;

How to use Union in Linq Query

I have many tables.But there are Two columns common in each table.
They are RegNo and Total.Now i want the values of all the total column for a particular RegNo.
I can get that in different queries like this.
query=from k in db.MyTable1 where K.regNo=1 select k.Total
query2=from k in db.MyTable2 where K.regNo=1 select k.Total
This way but i want to do this and get the Summation of all Total's Column using one single Query
Please guide.
You can do it this way.
var itemCounts = (from k in db.MyTable1 where k.RegNO==1 select k.Total)
.Union(from k in db.MyTable2 where k.RegNO==1 select k.Total);
TotalOfAll=itemCounts.Sum();
and using the sum method you can get the summation of all the Values in the query.

Resources