Let's say I have a table with the columns country, continent and population.
How can I use the QUERY function in Google Spreadsheets to select, for each continent, only the country with the highest population?
In regular SQL I think I'd use HAVING, but this doesn't seem to be an option here.
=SORTN(SORT(G:J,4,0),2^99,2,3,0)
SORT by population in descending order,if not done already
Remove Duplicates with SORTN
I suggest a helper column, say K with:
=if(maxifs(J:J,I:I,I2)=J2,"#","")
in K2 and copied down to suit, then:
=query(G:K,"select I,H,J where K is not NULL")
A couple of suggestions (both a bit long) where I've got Rank, Country and Continent in columns A, B and C (sorry it would have taken me too long to type in the populations)
To get a list in descending order of rank:
=ArrayFormula({unique(filter(C:C,C:C<>"")),
vlookup(Query(A:C,"select min(A) where A is not null group by C order by min(A) label min(A) 'Rank'"),A:C,2,false)})
To get a list in alphabetical order of continent:
=ArrayFormula({Query(A:C,"select C,min(A) where A is not null group by C label min(A) 'Rank'"),
vlookup(Query(A:C,"select min(A) where A is not null group by C label min(A) 'Rank'"),A:C,2,false)})
Although the list (I imagine) would go all the way down to Vatican City (pop about 1,000) most countries have at least several thousand inhabitants so I guess ties are pretty unlikely :-)
UPDATE - with population data
In QUERY method Select Continent, maximum of the population and need to group by Continent:
=QUERY(Countries,"select I, max(J) GROUP BY I",1)
This will return the below results from the table:
Continent max Population
Africa 173615345
Asia 1385566537
Australia 23342553
Europe 82726626
North America 320050716
South America 200361925
Related
first time posting here!
I have the following scenario:
1 Google Sheet with information sorted in tables (Master Data)
1 Google Sheet that =Importrange the data from the Master Data Google Sheet.
I need to import one time, or multiple times, some of the rows that are Imported from the Master Data based on the following criteria:
On the Master Data Google Sheet, a column would be present, showing in which Country/Countries the student lives. If the student lives 1 country, import the row once. If it is in 2,3,4... countries, import the same row it 2,3,4... times.
Right now, I am using the following formula:
=QUERY({IMPORTRANGE(Reference!A8,Reference!$A$2&Reference!B6)},"select Col6 where Col10='"&'Advanced Settings'!B5&"'")
This formula Imports from the Master Data file (Reference!A8), a particular tab (Reference!$A$2) and a particular range in this tab Reference!B6. Finally, it filters the data imported (only the 6th Col of the range, and only if on Col 10 the row has a particular value (Advanced Settings'!B5).
Is there a way to Import the name of the student as many times as countries they live in inside the same Array formula?
Right now, I am just adding more importrange (if there are 3 countries, I will add Importrange three times) with filters, but I would like to make it dynamic for the number of countries, without manual input every time. Also, the number of students imported varies every time so I can't look manually at the number of rows and then add a formula after the last cell of the array formula.
Thanks!
EDIT:
Sample Data and expected result:
Sample Data
Student Name
Gender
Class Level
Home State
Country
Alexandra
Female
4. Senior
CA
UK, US
Andrew
Male
1. Freshman
SD
UK
Anna
Female
1. Freshman
NC
UK, US
Becky
Female
4. Senior
SD
US
Benjamin
Male
4. Senior
WI
UK
Filter on both Class Level (4. Senior) and Country
Name
Reason for appearing (explanation for you)
Alexandra
Appears because Alexandra is Senior, UK
Alexandra
Appears because Alexandra is Senior, US
Becky
Appears because Becky is Senior, US
Benjamin
Appears because Benjamin is Senior, UK
The expected result here is that Alexandra appears twice as she's Senior and both US and UK.
if Reference!B6 is a range and IMPORTRANGE for each country is the same try:
=QUERY({IMPORTRANGE(Reference!A8, Reference!A2&Reference!B6)},
"select Col6
where Col10 matches '"&TEXTJOIN("|", 1, 'Advanced Settings'!B5:B)&"'", )
IF C = Senior and E is splittable, create a array of corresponding As. Then, FLATTEN the array and REDUCE to remove all empty items in the array.
Sample:
=ARRAYFORMULA(
REDUCE(
"Senior List",
FLATTEN(
IF(
C2:C6="4. Senior",
IF(ISTEXT(SPLIT(E2:E6,",")),A2:A6,),
)
),
LAMBDA(a,c,IF(c="",a,{a;c}))
)
)
I'm looking for a specific Google Sheets formula. I believe it's using COUNTIFS & SUMIF, but I can't work out how to do it, any help would be really appreciated.
So, my "C" column has a number of letter combinations in each cell... These are "FHG" "CS" "MO" and afew others. They are a reference to specific market trading strategies and I'm using this sheet to track every trade I make. So C3 may contain "FHG" C4, "MO" C5, "FHG", C6 "FHG" etc.
In the "I" column, there is a 'profit and loss' column which states how much was generated/lost for that particular row... These are formatted to currency.
At the top of my sheet, I have a summary list of each set of letters and I'm using a COUNTIF statement to work out the total number (instance count) of each particular letter combo and from there, I can work out strike rates, percentages etc.
The thing I can't work out is, 'is there a way to count/sum the relative numbers in the "I" column based on a specific letter combo?' For example, let's say running down today's trades, FHG appears on C3, C6, C8, C12, C16 etc. can I collect and sum the numbers (to create a total) from the corresponding P&L column, I3, I6, I8, I12, I16 etc?
My main aim is to have the following at the top in my "Summary Section":
'Letter Combo/Name Of Trading Technique' - 'Number Of Trades' (Using COUNTIFS down C Column) - 'Total % Win Rate' (Number Of Winners/Number Of Total Trades For Said Strategy - This is done using a separate column of Y or N for winners or losers) - The one that I need help with is - "Total Sum Of Money Generated For Each Particular Letter Combo (Trading Strategy)"...... I've worked out how to do the first 3 columns, but I can't work out how to pick the numbers out from the P&L column (I) based on what kind of trade (letter combo) exists in the corresponding (C) column.
If every trade made the same about of money, this would probably be an easy calculation, but because each return is different, this is proving challenging.
Thanks for any insights :)
try:
=QUERY(A:I; "select C,sum(I) where A is not null group by C label sum(I)''")
or just for SB:
=QUERY(A:I; "select C,sum(I) where C = 'SB' group by C label sum(I)''")
if you want a count too:
=QUERY(A:I; "select C,count(C),sum(I) where A is not null group by C")
I'm working on a different Google Sheets spreadsheet to input data (film details such as English Title, Original Title, Release Date, Rating, Country of Origin and a Link), while on the one I'm analyzing the data I managed to use importrange successfully.
Here is the code I used successfully in order to get the average rating of a country's list of movies:
=AVERAGE(IMPORTRANGE("LINK_TO_INPUT_DATA_GOOGLE_SHEETS", CONCAT(A1:A, "!D1:D")))
This average is outputted to column C, while the name of the Country (which is also the name of the sheet for importrange) is in Column A.
I want to create a similar query but for movies that have the Country of Origin matching the Country from Column A (Any movie that has multiple countries of origin are inputted with the first one in the spreadsheet and copied over in all the other countries of origin's respective sheets).
I tried using the QUERY from Google Sheets to make my resultset, but in the best case scenario, it gives the same result as the previous average, while in the worst case scenario it just gives out errors. Here is my latest attempt at the query:
=AVERAGE(QUERY (IMPORTRANGE("LINK_TO_INPUT_DATA_GOOGLE_SHEETS", A1:A), "SELECT Col4 WHERE Col5="&A1&""))
As far as I can tell, this should work, but at the moment it says it cannot find the range or sheet for the imported range.
Any help is deeply appreciated!
EDIT:
Here's a link of the input sheet: https://docs.google.com/spreadsheets/d/1bopmJu7Av71sCh8iUoG20WubGL9ssx09dOnBZnys4Ko/edit?usp=sharing
Here's a link of the analysis spreadsheet (the query should be in the MOVIES sheet):
https://docs.google.com/spreadsheets/d/1-hfQdqvDWXXtGR2fmTy-lZEOtp9sdxkvoget4toi1W4/edit?usp=sharing
I am not sure If I got you right:
- you want: import the average of the ratings (column 4) by movie title (column 1) where the country matches your current column A?
If so it can simply done with queries, especially if you include the average in the query as well. But you need to include all columns you use in the importrange:
=QUERY(IMPORTRANGE("https://...", "Syria!A1:E"),"SELECT AVG(Col4) WHERE Col5='"&A2&"' LABEL AVG(Col4) ''")
Explanation: group by will aggregate all columns by the column you declared as being used as average.
this is the correct syntax:
=AVERAGE(QUERY(IMPORTRANGE("ID_OR_URL"; "Sheet1!A1:A"); "SELECT Col4 WHERE Col5='"&A1&"'"; 0))
I have a Google Spreadsheet with two sheets.
In sheet "Source" I have a series of countries, cities and landmarks - these are,respectively, in columns A, B and C.
In sheet "Sheet for Query", there are two columns: (A) Country, which has a list of unique country names; and (B) Top 3 cities by Landmark. In column B, I would like to have a Query which gives me, for each country, the top three cities by number of landmark, i.e., the query just has to count the number of instances each city in each country appears and return, for each country, the names of the three cities that come up the most times
This is a sample sheet that I've created in order to demonstrate what I mean: https://docs.google.com/spreadsheets/d/1IPwtAHjwjV1A03o9URws-AtDKw3h9QS9UTT0P1PeVN0/edit?usp=sharing.
Thank you!
I've given this some thought and to 'just' count the number of instances and return the top 3 in each country is surprisingly difficult.
The grouping is straightforward with a query like this
=query(A:C," select A,B,count(C) where A<>'' group by A,B order by A,count(C) desc label A 'Country',B 'City', Count(C) 'Landmarks'",1)
But I don't know of a way of getting the top 3 for each group without going through 2 further steps
(1) Number the results in each group (various ways of doing it but here is one)
=(E1=E2)*D1+1
where the country names after grouping are in column E.
(2) Filter the result for the number in column D being less than 4
=filter(E:G,D:D<4)
You don't specify what qualifies as top (so assuming those are the first listed - higher up the sheet), and you don't clarify number of landmark where there are no numbers in your sheet, but perhaps:
=textjoin(", ",,query(Source!A:C,"select B where A='"&A2&"' limit 3"))
in B2 of sheet for Query, copied down to suit.
sample attendance (google) sheet
I want count the number of total lates per student name on this sample sheet, e.g, how many lates Mary Love has (actual sheet has over 30,000 rows, 10 columns). If possible, this count needs to change as students have additional lates. I'd really appreciate any help someone might be able to provide. I thank you very much in advance.
In addition to previous post, a QUERY() function also seems a good option as it can output a 2D-array, containing the names and the counts in one formula (no need to drag down). As an example, try:
=query(A:G, "select A, count(B) where B ='Late' group by A ", 1)
If you want to limit the result tho those who had more then 3 late's, you can do:
=query(query(A:G, "select A, count(B) where B ='Late' group by A ", 1), "where Col2 > 3")
Also further filtering with date can be done very easily. However I think that may require some 'cleaning up' of the current data: I noticed a lot of different date formats in col D... :-)
For counting each of several students and to allow greater versatility I suggest a pivot table with Student Name for Rows, Late for Values with Summarise by: COUNTA and Late for Filter with Show: Late.
You can use the COUNTIFS function to get what you are looking for. This formula will count all matches for multiple criteria ranges and criteria (e.g. student name & late).
This is the syntax for the formula:
COUNTIFS(criteria_range1, criteria1, [criteria_range2, criteria2]…)
If you put Mary Love in cell I2, you could then put this in J2.
=countifs(B:B,"=late", A:A,I2)
Then as more rows are added it will automatically update the "lates". I would also suggest using named ranges, then you could replace B:B and A:A with the named range.
I would suggest using the =query function as documented below. If that doesn't meet your needs you could modify the above:
=if (countifs(B:B,"=Late", A:A,I2, D:D, ">="&N2) >= 3, "True", "False")
This will put "True" in the column if there are 3 or more lates, false if not. This will require you put the date you want to check against in column N2. Change N2 above if you want to use a different column.