select DISTINCT country, count(river)
from geo_river
group by country
having count(river) > 3
INTERSECT
select DISTINCT country, count(province)
from geo_lake
group by lake,country
having count(province) > 3;
I'm sure USA is in it, but the result shows nothing, where is wrong?
I can't add pictures.
river_geo has river, country, province.
lake_geo has lake, country, province
"have lakes next to more than three provinces." means the lake has more than 3 province but these province belongs to the same country.
I want to find country that have this kind of lakes and have more than 3 rivers
Related
Sheet contains table and 9 query problem set.
https://docs.google.com/spreadsheets/d/1-YW7prEz2rkCKangks2CeDK2spCidGQa8hJJhXWmGiI/edit?usp=sharing
Trying to solve for each customer request:
1 For Mike, into one cell, list Title and Rating for Title that is specifically "Where is the Sun?" List only those with Rental Date that is not TODAY(), which is 9/1/2022
2 For Lizzy, into one cell with results sorted by latest Rental Date, find all Rental Dates, Ratings, and Elements, specifically by her Favorite Title: Where is the Venus?
3 For Ed, his favorite element is specifically "A", query Rental Date, Source, Title, into one Cell AND/OR bring back Rental Date, Source, Title for rating that is exactly 10.00%
4 For John, find Source, Title, Rating into one cell, sorted by oldest Rental Date, that contain "A" in element but those elements can never contain "K"
5 For Mona, find the Rental Date, Title, and Elements for the lowest rating. In another cell, do the same but find for the highest rating.
6 For Claire, find the Title, Rental Date, and Elements for highest rated title that is "Where is Venus"
7 For Frank, find the Title, Rental Date, and Elements whose Rating is closest to the Average Rating. In another cell, do the same for the the Rating that is farthest from the Average Rating.
8 For Jack, find the Rental Date and Title whose Rating is the biggest outlier in the range of Rating?
9 For Mina, list all Titles that have duplicate Rental Dates, Group titles and state their duplicate duplate Rental Dates.
your A2 cell is 9/1/2002 not 9/1/2022 (TODAY)
1:
=ARRAYFORMULA(TRIM(QUERY(CHAR(10)&FLATTEN(QUERY(TRANSPOSE(QUERY(A:E, "select C,D where C='Where is the Sun?' and A < date '"&TEXT(TODAY(), "e-m-d")&"'", 0)),,9^9)),,9^9)))
2:
=ARRAYFORMULA(TRIM(QUERY(CHAR(10)&FLATTEN(QUERY(TRANSPOSE(QUERY(A:E, "select A,D,E where C='Where is Venus?' order by A desc", 0)),,9^9)),,9^9)))
3:
=ARRAYFORMULA(TRIM(QUERY(CHAR(10)&FLATTEN(QUERY(TRANSPOSE(QUERY(A:E, "select A,B,C where E matches '.*(A,|, ?A$).*' and D = 0.1", 0)),,9^9)),,9^9)))
4:
=ARRAYFORMULA(TRIM(QUERY(CHAR(10)&FLATTEN(QUERY(TRANSPOSE(QUERY(A:E, "select B,C,D where E matches '.*(A,|, ?A$).*' and not E matches '.*(K,|, ?K$).*' order by A desc", 0)),,9^9)),,9^9)))
5:
=ARRAYFORMULA(TRIM({QUERY(CHAR(10)&FLATTEN(QUERY(TRANSPOSE(QUERY(A:E, "select A,C,E where D ="&MIN(D2:D), 0)),,9^9)),,9^9), QUERY(CHAR(10)&FLATTEN(QUERY(TRANSPOSE(QUERY(A:E, "select A,C,E where D ="&MAX(D2:D), 0)),,9^9)),,9^9)}))
6:
=ARRAYFORMULA(TRIM(QUERY(CHAR(10)&FLATTEN(QUERY(TRANSPOSE(QUERY(A:E, "select C,A,E where C = 'Where is Venus?' order by D desc limit 1", 0)),,9^9)),,9^9)))
7:
=ARRAYFORMULA(TRIM({QUERY(CHAR(10)&FLATTEN(QUERY(TRANSPOSE(QUERY({A2:E, ABS(D2:D-AVERAGE(D2:D))&""}, "select Col3,Col1,Col5 where Col6 ='"&MIN(ABS(D2:D-AVERAGE(D2:D)))&"'", 0)),,9^9)),,9^9), QUERY(CHAR(10)&FLATTEN(QUERY(TRANSPOSE(QUERY({A2:E, ABS(D2:D-AVERAGE(D2:D))&""}, "select Col3,Col1,Col5 where Col6 ='"&MAX(ABS(D2:D-AVERAGE(D2:D)))&"'", 0)),,9^9)),,9^9)}))
8:
no idea what "biggest outlier in the range of Rating" is. if you want that negative value just say so...
9:
kindly provide an example of the desired result in your sample sheet
I'm a DAX beginner using it in the context of a data model in Excel. I have a large dataset with several tens of thousands of rows split into various groups with record numbers, classes, year, Group name, location, status and lots of other categories of data.
I'd like to add some specific calculated columns for particular key filters such as the location with the largest sales within each Group where the source is still live, and have a measure that works for whichever fields I bring into a pivot (e.g. if I added year, source, state or any combination etc)
For example in the simplified table below I'd like to calculate a column that returns the dominant location for each Group by Sales, subject to only including live records. So for Group A I'd add up all sales for USA and Canada where the Status is live, then take whichever category is largest (Canada for Group A, USA for Group B) and add it as the Dominant location as below:
Group
Location
Status
Sales
Dominant Location
Group A
USA
Live
500
Canada
Group A
USA
Disc
250
Canada
Group A
USA
Disc
290
Canada
Group A
Canada
Live
875
Canada
Group A
Canada
Live
115
Canada
Group A
USA
Live
310
Canada
Group B
USA
Live
310
USA
Group B
UK
Live
285
USA
Group B
UK
Live
705
USA
Group B
USA
Live
430
USA
Group B
USA
Live
670
USA
My original thought process was to use GroupBy to return a table of live sales by location for each group and then use topn to select the largest location by live sales and then SelectColumns to return the location although this doesn't appear to work (or is coded wrongly). Example failed attempt below:
=SELECTCOLUMNS( TOPN(1, GROUPBY('Table',[Location],"Live Sales",SUMX(CURRENTGROUP(),[Sales]* IF([Status]="Live",1,0)) ),[Live GNWP]) ,"Location",[Live GNWP] )
I have managed to add this column with an intermediate calculated column using:
Intermediate=CALCULATE(SUM([Sales]), FILTER('Table', EARLIER([Group]) = [Group] && [Status]="Live" && EARLIER([Location] = [Location])))
Then
=CALCULATE (
FIRSTNONBLANK (
TOPN ( 1,
VALUES ('Table'[Location] ),
CALCULATE ( MAX ( [Intermediate]) )
), 0 ), ALLEXCEPT ( 'Table','Table'[Group] ) )
This does work as a calculated column, but my actual data is too large in row count and field count to be adding extra intermediate calculated fields every time I want a new criteria for selecting the largest category. I also cannot replicate this as a measure so would appreciate understanding why my initial method didn't work and if it's possible to generate this field as a calculated column in one step and then additionally how to get it to work as a measure.
Here's one way to write a corresponding measure without intermediate calculated columns:
Dominant Location =
VAR Summary =
SUMMARIZE (
FILTER ( ALLEXCEPT ( 'Table', 'Table'[Group] ), 'Table'[Status] = "Live" ),
'Table'[Location],
"#SumSales", SUM ( 'Table'[Sales] )
)
RETURN
SELECTCOLUMNS (
TOPN ( 1, Summary, [#SumSales], DESC ),
"Location", 'Table'[Location]
)
I need write a query to get top 10 Items sold in each country
Database Type is MS Access 2013
Table Name: SoldItems
Fields
Country
Item
Query
This query will get me all items sold in each country ordered descending by count of sold items in each country, i only need top 10 items sold in each country
SELECT count(*) As CountOfItemsSold, Country, Item
from SoldItems
group by Country, Item
order by 2,1 desc
You will need to use a sub query that returns the Top 10 items grouped by country and use that as the basis for a filter. Something like:
SELECT S.Country, S.Item, COUNT(S.Item) AS CountOfItem
FROM SoldItems AS S
GROUP BY S.Country, S.Item
HAVING S.Item In (SELECT TOP 10 R.Item
FROM SoldItems AS R
GROUP BY R.Country, R.Item
HAVING R.Country=S.Country
ORDER BY COUNT(R.Item) DESC
)
ORDER BY S.Country, COUNT(S.Item) DESC;
Note that if there are items with an equal number of items sold, then you will get more than 10 records returned per country.
Regards,
I have a table created. With one column named states and another column called land area. I am using oracle 11g. I have looked at various questions on here and cannot find a solution. Here is what I have tried so far:
SELECT LandAreas, State
FROM ( SELECT LandAreas, State, DENSE_RANK() OVER (ORDER BY State DESC) sal_dense_rank
FROM Map )
WHERE sal_dense_rank >= 5;
This does not provide the top 5 land areas as far as number wise.
I have also tried this one but no go either:
SELECT * FROM Map order by State desc)
where rownum < 5;
Anyone have any suggestions to get me on the right track??
Here is a samle of the table
states land areas
michagan 15000
florida 25000
tennessee 10000
alabama 80000
new york 150000
california 20000
oregon 5000
texas 6000
utah 3000
nebraska 1000
Desired output from query:
States land area
new york 150000
alabama 80000
florida 25000
california 20000
Try:
Select * from
(SELECT State, LandAreas FROM Map ORDER BY LandAreas DESC)
where rownum < 6
Link to Fiddle
Use a HAVING clause and count the number state states larger:
SELECT m.state, m.landArea
FROM Map m
LEFT JOIN Map m2 on m2.landArea > m.landArea
GROUP BY m.state, m.landArea
HAVING count(*) < 5
ORDER BY m.landArea DESC
See SQLFiddle
This joins each state to every state whose area is greater, then uses a HAVING clause to return only those states where the number of larger states was less than 5.
Ties are all returned, leading to more than 5 rows in the case of a tie for 5th.
The left join is needed for the case of the largest state, which has no other larger state to join to.
The ORDER BY is optional.
Try something like this
select m.states,m.landarea
from map m
where (select count(‘x’) from map m2 where m2.landarea > m.landarea)<=5
order by m.landarea
There are two bloomers in your posted code.
You need to use landarea in the DENSE_RANK() call. At the moment you're ordering the states in reverse alphabetical order.
Your filter in the outer query is the wrong way around: you're excluding the top four results.
Here is what you need ...
SELECT LandArea, State
FROM ( SELECT LandArea
, State
, DENSE_RANK() OVER (ORDER BY landarea DESC) as area_dr
FROM Maps )
WHERE area_dr <= 5
order by area_dr;
... and here is the SQL Fiddle to prove it. (I'm going with the statement in the question that you want the top 5 biggest states and ignoring the fact that your desired result set has only four rows. But adjust the outer filter as you will).
There are three different functions for deriving top-N result sets: DENSE_RANK, RANK and ROW_NUMBER.
Using ROW_NUMBER will always guarantee you 5 rows in the result set, but you may get the wrong result if there are several states with the same land area (unlikely in this case, but other data sets will produce such clashes). So: 1,2,3,4,5
The difference between RANK and DENSE_RANK is how they handle ties. DENSE_RANK always produces a series of consecutive numbers, regardless of how many rows there are in each rank. So: 1,2,2,3,3,3,4,5
RANK on the other hand will produce a sparse series if a given rank has more than one hit. So: 1,2,2,4,4,4.
Note that each of the example result sets has a different number of rows. Which one is correct? It depends on the precise question you want to ask.
Using a sorted sub-query with the ROWNUM pseudo-column will work like the ROW_NUMBER function, but I prefer using ROW_NUMBER because it is more powerful and more error-proof.
I have been asked this question;
You list county names and the surnames of the representatives if the representatives in the counties have the same surname.
and I have the following tables;
***REPRESENTATIVE***
REPI SURNAME FIRSTNAME COUNTY CONS
---- ---------- ---------- ---------- ----
R100 Gorege Larry kent CON1
R101 shneebly john kent CON2
R102 shneebly steve kent CON3
I cant seem to figure out the correct way to ask Orical to display a surname that exists more then twice and the surnames are in the same country.
I know how to ask WHERE something = something, but that's doesn't ask what I want to know.
It sounds like you want to use the HAVING clause after doing a GROUP BY
SELECT surname, county, count(*)
FROM you_table
GROUP BY surname, county
HAVING count(*) > 1;
If you really mean "more than twice" as you wrote, none of the data you'd want HAVING count(*) > 2 but then none of your sample data would be returned.
In words, this SQL statement says
Group the data into buckets by surname and county. Each distinct combination of surname and county is a separate bucket.
Count the number of rows in each bucket
Return those buckets where there are at least two rows