In Google Spreadsheet, I have the expression:
=QUERY(database; "select b,c, where a='anyone-condition' order by c desc";-1)
The column a,b are strings and c is a number.
How can I include in this query one calculated field, c/sum(c)?
Although Ed Nelson is perfectly right, you can be creative with multiple nested Queries and get desire result without an extra column.
I have build simple example that will show you the way
Data
Solution 1 - simpler but with ugly column name
=QUERY(
A1:C3,"select A,B,C/"&QUERY(
QUERY(A1:C3,"select sum(C)"),"select * offset 1",0)&"" ,1
)
Solution 1.1 - more complex but with custom column name
=QUERY(
A1:C3,"select A,B,C/"&QUERY(
QUERY(A1:C3,"select sum(C)"),"select * offset 1",0)&
" label C/"&QUERY(
QUERY(A1:C3,"select sum(C)"),"select * offset 1",0)&
" 'C/sum'",1
)
Finals:
You can go further and add formating to last column as well
Link to working copy
For your specific example probably code should be like this:
=query(database; "select b,"&query(query(database; "select sum(c), where a='anyone-condition' "),"select * offset 1",0)&" where a='anyone-condition' order by c desc";-1)
But, it should be tested against real data
Is that serves your needs?
You can't do a sum without doing a group which you don't want. You can add a column D to your sheet to sum. Try =sum($C$2:$C) in D and copy down. Then this will work:
=query(database, "select B,C/D where A='anyone-condition' order by C desc label C/D ''",-1)
The problem is with the Google Spreadsheet 'kernel', not with my coding. Look the scream that I printed
https://drive.google.com/file/d/1yJbcfVZ1tDmW8WoG224PALRIwyEbX9LH/view?usp=sharing
Related
I'm building a Google Sheet table and I am stuck on a specific query I want to make
Source Table pic here
I need to sort my Names by "Type" and by "ValueType".
I managed to sort them by "Type" with ease, but i'm stuck on the sort by "ValueType" part, because they are in columns, not lines and I can't manage to find a way to sort them by columns
My Query looks like this right now
=QUERY(A1:G8; "SELECT * WHERE A='Type1'; 1)
I want it to look something like this :
=QUERY(A1:G8; "SELECT * WHERE A='Type1' AND C1:G1='ValueType 1'"; 1)
Is it possible to do something like this, and if so, can you please tell me what is the syntax?
Thanks in advance.
=QUERY(A1:G8; "SELECT * WHERE A='Type1' AND (C='"&C$1&"' or D='"&D$1&"') "; 1)
adding or X='"&X$1&"' within the () where X is the next letter.
'"&C$1&"' is the value in cell C$1 but you can adapt that for a different value on your sheet, or a fixed value using C='xxx'
You need to expand the formula, or you can do it too (if the number of columns is not fixed)
=QUERY({A1:G8}, "SELECT * WHERE Col1='Type1' and (" & "Col"&arrayformula(TEXTJOIN("='ValueType 1' or Col",,column(C:G)))&"='ValueType 1' )", 1)
try just that sentence to understand how it has been built
="Col"&arrayformula(TEXTJOIN("='ValueType 1' or Col",,column(C:G)))&"='ValueType 1'"
Hello I need help with named range in google sheet...
=QUERY(Data,"SELECT C,D,E,SUM(G),L,SUM(G*M) WHERE A = '"&$A$1&"' AND E = '"&$B$1&"' GROUP BY C,D,E,L",1)
I'd like to calculate columnG*columnM and than SUM this results when grouping.
This works fine before I start grouping table. I could do columnG*columnM get result and use another QUERY but don't like the idea of using many helpers. Is it even possible?
Could you provide a link to your project ? I make a simplified example : if you need the sum of ColumnB x ColumnC, you have to add to your data an extra column as following
=query({A2:C,arrayformula(B2:B*C2:C)},"select SUM(Col4) group by Col1 ")
and then usie ColX instead of A,B,C and the extra column D.
I have a CSV file that I'm pulling from a database. It's in an awkward layout so I need to reorganise it and display the result in a separate sheet.
Here is a dummy example of the data structure I get.
https://docs.google.com/spreadsheets/d/1sTfjr-rd0vMIeb3qgBaq9SC8felJ1Pb4Vk_fMNXQKQg/edit?usp=sharing
It looks like that. The database grows every day by date and sometimes countries so I need to account to that in my formula.
I need to pull data per each country and display it by date.
I don't need data from Column A, C and D. And when there are multiple states I need to sum them up in one column.
It should look like this and keep growing downwards. I'm gonna use this table for a graph chart
What I've tried so far
=TRANSPOSE(QUERY(IMPORTRANGE("url_to_a_separate_sheet_where_I_importing_a_row_csv_file", "CSV-source-sheet!A1:500"), "SELECT * WHERE Col2='Germany'"))
This works, kinda. But pulls in unnecessary columns and I can't figure out how to sum countries with multiple states. When I add select sum(*) it gives me a big and long error. I assume it might be because of unnecessary columns that the formula cant sum up and I don't know how to omit them. I'm stuck
I tried offset and skipping no luck. Any ideas?
try:
=ARRAYFORMULA(TRANSPOSE(QUERY({Sheet2!B:B, Sheet2!E:BE},
"select Col1,"&TEXTJOIN(",", 1,
"sum(Col"&ROW(INDIRECT("Sheet2!A2:A"&COUNTA(Sheet2!1:1)-5))&")")&"
where Col1 is not null
group by Col1
label Col1'Date'", 1)))
spreadsheet demo
We have a base table that looks like this, in our main sheet called "MainData".
We would like to summarize it in a new worksheet. The summary needs to be by time, in a program management mode, where the "When" becomes the main view, in the following way. We could technically get a version of the top part of the table via Pivot, but that forces a new worksheet. We would like this entire view to be in our own second sheet of choice which we can call "Summary".
Not sure where to begin with this. The GETPIVOTDATA command seems a more convenient way to control how the pivot shows without forcing a worksheet, but it's the itemization of colours etc that is confusing. In each week's listing below that week column, we'd like to show the items but their cell needs to be coloured by the Status that item is in.
Not looking for ready made solutions (although I won't revolt if that's shared), just looking for pointers for which functions to look for. Thanks muchly!
Solution:
It's quite a task... :)
I have build working solution for you.
Go to this link to grab this (2 sheets - data and report)
Explanation:
Data sheet:
I added an extra column to source data - we will need this column in further query (you can hide this column)
={"Rep Desc";ArrayFormula(if(A2:A<>"";"Count of "&A2:A;))}
Report sheet:
I added 2 extra columns (A:B) (you can hide them later) to explain better what is going on. There are 4 main parts to this solution - you are able to pack all of them into one formula, but for sake of clarification I left them separate.
Part 1 Numbers of "Open / Closes / Attn"
This is simple query - we use extra column in data source to have desire description (Count of... instead just Attn, Closed, etc)
=QUERY({INDIRECT($A$1)};$B$1;1)
string to query
select Col5, count(Col4) where Col1 is not null group by Col5 pivot Col3 label Col5 ''
Part 2 - "Sum of Point"
Its Query again put into next query to remove headers + "Sum of Points" as an extra column (using inline array - {}):
={"Sum of Points"\QUERY(QUERY({INDIRECT($A$1)};B5;1);"select * offset 1";0)}
string to query
select sum(Col4) where Col1 is not null pivot Col3
Part 3 - "Features"
It is quite complicated... If I find more time I will describe what is going on here... but for now just code:
=QUERY(
transpose(ArrayFormula(SPLIT(
transpose(SPLIT(
TEXTJOIN("^";1;transpose(
{SPLIT(join(" ## ";transpose(query(transpose(QUERY({INDIRECT($A$1)};$B$9;1));"select Col1 offset 1";0)));" #";0;1);
QUERY(ArrayFormula(IF(TRANSPOSE(query(transpose(QUERY({INDIRECT($A$1)};$B$9;1));"select * offset 1";0))<>"";
query(QUERY({INDIRECT($A$1)};$B$9;1);"select Col1";0);""));"select * offset 1";0)}
))
;"# ";0;1))
;"^")))
;
"select * offset 1";0)
Part 4 - Conditional formatting
For range D9:H apply 3 rules with corresponding color :
=INDEX(INDIRECT("data!$A:$A");MATCH(D9;INDIRECT("data!$B:$B");0);1)="Open"
=INDEX(INDIRECT("data!$A:$A");MATCH(D9;INDIRECT("data!$B:$B");0);1)="Closed"
=INDEX(INDIRECT("data!$A:$A");MATCH(D9;INDIRECT("data!$B:$B");0);1)="Attn"
OK?
Is that what you were going to achieve?
Again - this is working copy for you:
Go to this link to grab this (2 sheets - data and report)
Testing Sheet:
Wondering if there is a witty way to add a Total to the last row +1 of
a Query result.
See Sheet 'Lookup' for a static example of what I am asking for.
I don't know if there is a way to have a hidden column that calculates
transposed only under the last row of a query, or if there is a smart
way to work Query for this answer.
All great answers. Each on very useful in its use case.
Макс Махров gets the answer with using a query statement.
Now I was not keen on having an extra sheet to hold the totals so I added a row at the top which I can simply hide and used this formula:
query({Orders!A:E;A1:E1},"select Col1, Col3, Col4 where Col2 = '"&C3&"' order by Col4",1)
Only problem I have is trying to figure out how to add TEXT to the bottom row, it seems to only want numerical input.
How do I fix this? What am I glitching?
Thanks !
Mars
The trick is to make second query and count totals for selected product.
Plan of actions:
add new sheet with query on it, something like this: =QUERY(Orders!A:E,"select B, 0, sum(D) where B like '"&Lookup!C2&"' Group by B",0)
Prepare arrayformula which combines data in Lookup sheet: = ArrayFormula({Importrange(1),Importrange(2)}) Note that number of columns must retain the same.
Edit query so it takes Col1, Col2, Col3... instead of A, B, C...
Make word 'total' visible instead of zero. Set number format: 0;0;total Set it for range B9:B on Lookup sheet
Make Conditional Formatting with formula =and($B4 =0,isnumber($B4)) for range A4:C on Lookup sheet.
That's seems have to complete the task.
Hope it Helps!
Your Example
Working example.
Here is one way:
Put TOTAL way down in row 1000
Select the range A3:C999. Select data > filter to create filters
Select C3, set the filter to hide all blanks
A second way is to limit the query result to show only the top 8 results:
Change your query to =query(Orders!A:E, "select A, C, D where B = '"&C2&"' order by D desc limit 8",1) It will reverse-order column D (largest first), and set row limit to 8.
Change the formula of your TOTAL to =sumif(Orders!B:B,C2,Orders!D:D)
Try this formula in the column adjacent to your query:
=ArrayFormula({$C$4:offset($C$4,count($C$4:$C),0,1,1);sum($C$4:offset($C$4,count($C$4:$C),0,1,1))})
It duplicates your column of values (I haven't figured out a way around that yet) and then adds a total to the bottom of that column, and changes dynamically with the range from your query.
Here's a working version.
Interesting challenge! It got the old grey matter turning... ;)
Thanks,
Ben