Can I sort output of QUERY and include labels? - google-sheets

I'm trying to format my budget spreadsheet which updates automatically with an attached form. ColA is hidden since it's a timestamp, ColB is the date that the purchase was made, ColC is the category, and ColD is the amount.
I'm trying to sum all amounts within a category and have the output (in ColD) be sorted in descending order, as well as output the labels next to the amounts (in ColC). I have tried using SUMIF, but it's frustrating to use when I want to add multiple categories in specific ranges together. Here's the current code I'm using:
=QUERY(C3:D64, "SELECT SUM(D) WHERE C <> 'Income' GROUP BY C LABEL SUM(D)''")
This outputs my data, but without the labels and not in descending order. I can make the order descend in ColE with this code, but again, not labeled:
=QUERY(D65:D72, "SELECT D ORDER BY D")
Is there a way to simplify my code and have it output with labels in descending order, including labels for amounts where the output is 0? Here's a screenshot of my current setup.

If you want to include label (Column C), you need to select column C in your query.
Formula order based on column C name in descending order (See Cell F2):
=query(C3:D64,"Select C, sum(D) where C<>'Income' group by C order by C desc label sum(D) ''")
Formula order based on Sum of column D per category in descending order (See Cell I2):
=query(C3:D64,"Select C, sum(D) where C<>'Income' group by C order by Sum(D) desc label sum(D) ''")

You can add labels anyway, but make sure you keep proper order.
1 SELECT
2 WHERE
3 GROUP BY
4 ORDER BY
5 LABEL
(full list of commands is available in [documentation]: 1)
I am not sure what you mean by "Labels" -if you mean categories like "groceries, etc. you should use:
=QUERY(C3:D64, "SELECT C, SUM(D) WHERE C <> 'Income' GROUP BY C ORDER BY SUM (D) LABEL C 'Category', SUM(D) ''")
Sorry if I misanderstood your question

Related

Group values corresponding to different duplicated keywords in a query in Gsheets

I am trying to test some basic sums with large data sets with duplicated words in a row in this Gsheets Test duplicates
I like to get the top 4 suppliers by country, based on volume but I have the condition that suppliers are duplicated in different positions in different rows. A first approach was to remove duplicates by grouping within a query and just sum the Kgs, but that's missing some of the values as seen on the query cell result.
=query($A$3:$C$6;"SELECT SUM(C) WHERE A matches '.*Walmart.*' and B='USA' ORDER BY SUM(C) DESC LABEL SUM(C) 'Kgs'";0)
Any more efficient approaches?
try with group by:
=QUERY(A3:C6;
"select B,sum(C)
where A matches '.*Walmart.*'
and B='USA'
group by B
order by sum(C) desc
label sum(C) 'Kgs'"; 0)

How can I separate a column into multiple columns based on values?

I have searched on a lot of pages but I cannot find a solution to my problem except in reverse order. I have simplified what I do, but I have a query that comes looking for information in my data sheet. Here there are 3 columns, the date, the amount and the source.
I would like, with a query function, to be able to make different columns which counts the information of column C based on the values of its cells per month, like this
I'm okay with the start of the formula
=QUERY(A2:C,"select month(A)+1, sum(B), count(C) where A is not null group by month(A)+1")
But as soon as I try a little different things by putting 2 query together in an arrayformula, obviously the row count doesn't match as some minus are 0 for some sources.
Do you have a solution for what I'm trying to do? Thank you in advance :)
Solution:
It's not possible in Google Query Language to have a single query statement that has one result grouped by one column and another result grouped by another.
The first two columns can be like this:
=QUERY(A2:C,"select month(A)+1, sum(B) where A is not null group by month(A)+1 label month(A)+1 'Month', sum(B) 'Amount'")
To create the column labels for the succeeding columns, use in the first row, in my example, I1:
=TRANSPOSE(UNIQUE(C2:C))
Then from cell I2, enter this:
=COUNTIFS(arrayformula(month($A$2:$A)),$G2,$C$2:$C,I$1)
Then drag horizontally and vertically to apply to the entire table.
Results:
try:
=INDEX({
QUERY({MONTH(A2:A), B2:C},
"select Col1,sum(Col2) where Col2 is not null group by Col1 label Col1'month',sum(Col2)'amount'"),
QUERY({MONTH(A2:A), B2:C, C2:C},
"select count(Col3) where Col2 is not null group by Col1 pivot Col4")})

Google Sheets Combine a column with duplicates and update total sum in another colum

This might be something fairly simple but struggling to find a way to do it.
In Column B, I have a list of foods required.
In Column C, I have the amount needed.
In Column D, I have g (for grams) ml (for mills) etc.
I would like to combine the duplicates in Column B and update the totals from Column C, with the g or ml in Column D beside it.
The list I have has been created by using an array formula based on dropdowns in another sheet.
I have seen people using UNIQUE formula in 1 column (this works) and then a SUMIF formula in another column and then a JOIN formula in another... I tried this but the SUMIF is always returning 0.
Would someone please be able to advise on how I can do this?
TIA :D
It's hard to be sure exactly what you need without seeing the data. But based on my understanding of solely what you've posted, this QUERY formula should generate a condensed mini-report:
=QUERY({B2:D},"Select Col1, SUM(Col2), Col3 WHERE Col1 Is Not Null GROUP BY Col1, Col3 LABEL SUM(Col2) ''")
In plain English, this means "Arrange the data from the range B2:D in the same order as the raw data, but sum the second column's data according to matches in both the first and third columns. Only return results for the raw data where the first column is not blank. Replace the default 'sum' header on the second column with nothing; I don't need it."
This formula assumes that every ingredient will always be attached to the same measurement (e.g., 'salt' in Col B is always paired with 'mg' in Col D, etc.). If this is not the case, you will wind up with ingredients being listed as many times as there are different measures in Col D.

Extract two column values based on top 5 of one column, without grabbing the rest of the columns

I have a sheet with all kinds of values, the most important ones:
Column B2 to 256: Color name
Column F2 to 256: Sold items
I need to grab the top 5 color names and amount of sold items, without grabbing the rest of the columns.
You mention:
...grab the top 5 color names and amount of sold items, without grabbing the rest of the columns.
There are different ways to read your request
If your question is:
Grab the top 5 color names (meaning 5 colors with most sold items) and amount of sold items, use the formula in H3
=QUERY(A1:F,"select B, sum(F) where B is not null
group by B order by sum(F) desc limit 5 label sum(F) 'MOST Sold' ",1)
If your question is:
Grab the top 5 color names (meaning top 5 colors as they appear in column B) and amount of sold items, use the formula in H11
=QUERY(A1:F,"select B, sum(F) where B is not null and B matches '"&JOIN("|",(ARRAY_CONSTRAIN(UNIQUE(B2:B),5,1)))&"'
group by B order by sum(F) desc limit 5 label sum(F) 'MOST Sold' ",1)
(Please adjust ranges to your needs)
If I misunderstood your question, please share a test sheet so as you can be easier helped.
Functions used:
QUERY
Array_CONSTRAIN
JOIN
UNIQUE

Trying to include additional columns in QUERY result on GoogleSheets

I'm using Google Sheets and using the following formula without any issue:
=QUERY(Sheet!A:D,"select A, max(B) where not B is null group by A label A 'Client', max(B) 'Most Recent'")
Obviously, this is returning columns A and B of the referenced data
However, I would like to also return the corresponding cells from column C.
sample data: https://docs.google.com/spreadsheets/d/1CJKiDNPUWMMUbLLb-UDA6iRA2PBnlMHDsEB9vELe0Rk/edit?usp=sharing
Using this example, what I would like to see is cell G2 populated with "Pizza" (i.e., from C3), and so on.
Am I using the right formula? What do I need to change?
What you are trying to do is not very SQL-like, because max(B) does not point to any particular row: it's just the maximum value of B among the selected rows. This value could be attained by several rows, making the choice of C, D ambiguous. I don't think that a single query can do this.
Here is an implementation with unique and several query commands.
In E2, enter =unique(A:A) to get the list of unique client names.
In F2, enter
=query(A:D, "select B,C,D where A ='"&E2&"' order by B desc limit 1")
and drag this formula down. This selects all rows with the A value matching E2, and picks one with maximal B value.
You don't want to have a header row in the output of these queries so either add label B '', C '', D '' or just don't include the header row in the queried range.
Version with grouping by C,D
To also select C and D within a single query, expand the select clause
select A, max(B), C, D
which will then require including those in the group by clause:
group by A, C, D
The formula will be
=QUERY(A:D,"select A, max(B), C, D where not B is null group by A, C, D label A 'Client', max(B) 'Most Recent'")
This does mean that the only rows to be grouped together will be those where not only A but also C and D are equal.
A possible one-formula solution:
=ArrayFormula(IFERROR(VLOOKUP(UNIQUE(A2:A),SORT(A2:C,2,0),{1,2,3},0)))
Here is what I did that finally worked.
In E1, enter =unique(A:A) to get the list of unique client names. In F2, enter
=query(A$2:D101, "select B,C,D where A ='"&F2&"' order by B desc limit 1")
and drag this formula down. This selects all rows with the A value matching E2, and picks one with maximal B value.
You don't want to have a header row in the output of these queries so just add desired text in header row.

Resources