MySql Numbering the rows of query based on group by - mysql

I have a table like this
id name city
-------------------------------
1 Ian London
2 John London
3 David New York
4 Sylvia Mumbai
5 Beryl New York
6 Rashan London
I would like to retrieve the data with a row numbering that is grouped on City. Like this.
name city count
-------------------------------
Ian London 0
John London 1
Rashan London 2
Beryl New York 0
David New York 1
Sylvia Mumbai 0
I have been trying with
ROW_NUMBER() OVER (PARTITION BY City ORDER BY name)-1
But the count is of all the items returned, where I would like to count the number of people in each city.

Your code should do what you want - however, you mention group by, which is irrelevant here. The query should just be:
select name, city, row_number() over(partition by city order by name) - 1 rn
from mytable

To get the results that you specify:
select name, city, row_number() over (partition by city, order by name) as count
from t
order by count(*) over (partition by city) desc, -- number of rows in city
city,
name;
You seem to want the cities ordered by the number of names in the city.

Related

SQL groupby rollup vs union

Sql question:
enter image description here
competitor country
Acme Corp USA
GLOBEX USA
Openmedia France
K-bam USA
Hatdrill UK
Hexgreen Germany
D-ranron France
Faxla Spain
the output should be
country competitors
France 2
Germany 1
Spain 1
UK 1
USA 3
Total: 8
except using groupby with rollup, i am trying to solve it via "union" but turns out "order by is not functioning" (supposed to order by country name, but my output turns out to "order by competitors" ...)
This is my code:
(select country, count(competitor) as competitors
from table
group by 1
order by 1
)
union all
(select "Total:" as country, count(*) as competitors from table);
Any help would be appreciated! Thank you!
If you want the result ordered, you need an order by after the union:
(select country, count(competitor) as competitors
from table
group by 1
) union all
(select 'Total:' as country, count(*) as competitors
from table
)
order by (country = 'Total:') desc, country asc

Find MAX(Avg(value)) after grouped by two fields

The table name is c_list.
Country City Rating Date
------------------------------------
France Brest 95 24092016
France Brest 98 27092016
France Brest 95 03102016
France Lille 100 26092016
France Lille 92 28092016
Japan Tokyo 98 02102016
There are more than 50 different countries and each country have few cities. And each city may have more than one row of record or more. I want to select one City with highest average Rating (Compare to Cities in it's own country) and then compare with all other cities in different countries. So, the final query should display all Country and their ONE City with max(avg(Rating)) and in desc order. The sample output:
Country City max(avg(rating))
-------------------------------------
USA New York 97.25
UK Cardiff 96.70
Germany Greven 96.50
Turkey Afyon 94.88
France Guipavas 94.10
Canada Cartwright 91.35
I can only get the max(avg(rating)) for one country. Need help.
SELECT top 1 country, city, Avg(rating) AS Ratings
FROM c_list
where country = 'France'
GROUP BY city, country
order by Ratings desc
(Edited) The result that I want is similar like Miss world contest. Compete and win against local contestant in your country first. Next (my final result set) is to compete against the winners from other countries and rank them first to last using their avg(rating) that they got eatlier in their country.
If am not wrong you are looking for this
SELECT country,
city,
Avg(rating) AS Ratings
FROM c_list A
GROUP BY city,
country
HAVING Avg(rating) = (SELECT TOP 1 Avg(rating) AS Ratings
FROM c_list B
WHERE a.country = b.country
GROUP BY city
ORDER BY ratings DESC)
ORDER BY ratings DESC
Note : If you are using Mysql the replace TOP keyword with LIMIT
Base table:
select t.* from temp_lax t
COUNTRY CITY RATING
1 France Brest 95
2 France Brest 98
3 France Brest 95
4 France Lille 100
5 France Lille 92
6 Japan Tokyo 98
Query:
select t1.country, t1.city, Avg(t1.Rating) rating
from temp_lax t1
group by t1.country, t1.city
having avg(t1.rating) = (select max(avg(rating))
from temp_lax t2
WHERE t1.country = t2.country
GROUP BY t2.city)
order by rating desc
OUTPUT:
COUNTRY CITY RATING
1 Japan Tokyo 98
2 France Lille 96
3 France Brest 96
Please let me know if you are looking for different result set.

Get top 5 foreign keys

I have a table say City. I have state as a foreign key in it. What query I need to write to get the Top 5 states which occur in the city table.
Eg
Id City State
1 Mumbai Maharashtra
2 Pune Maharashtra
3 Amritsar Punjab
Here the output I expect is to get as names of the states with count
State_Name count
Maharashtra 2
Punjab 1
SELECT p.name, count(pr.State) FROM City pr
join state p on p.Id = pr.StateId;
Use Group by with count aggregate to count the state and use Limit to filter the records
select count(1) Cnt,state
from yourtable
group by state
order by Cnt desc Limit 5

SQL Query to find highest rated

SQL Query:
Aim:
Write a SQL query to retrieve the address of highest rated Phills
Coffee in United States addressing format.
id name house street city state zip country rating
1 Best Buy 34 Main St Carson CA 98064 USA 9
2 Phills Coffee 4568 Sepulveda Blvd Torrance CA 50833 USA 6
3 Starbucks 3 Ocean Blvd Long Beach WA 45093 USA 9
4 Phills Coffee 214 Carson St Huntington Beach PA 89435 USA 4
US Addressing Format (For people outside USA):
http://bitboost.com/ref/international-address-formats/united_states/
My attempt:
SELECT house, street, city,
state,country,zip
FROM table
WHERE name="Phills Coffee"
ORDER BY rating DESC LIMIT 1
Am I doing wrong? Or How can I improve this query?
Thanks,
US address format would be like:
4568 Sepulveda Blvd, Torrance, CA 50833 USA.
So your select would look like:
SELECT CONCAT(house, ' ', street, ', ', city, ', ', state, ' ', zip, ' ', country)
FROM table
WHERE name="Phills Coffee"
ORDER BY rating DESC LIMIT 1
You've been asked to retrieve the address, and in United States addressing format. So use that :
SELECT CONCAT(street,' ','country', ',') ...
Dont know what is the US addressing format, but use concat to get it done. Your WHERE condition and ORDER BY are OK
You will have to use Max(rating) As Highest Rating and Group By for this,
SELECT house, street, city,
state,country,zip,Max(rating)
FROM table
Group By house, street, city,
state,country,zip
Having name="Phills Coffee"
ORDER BY rating DESC LIMIT 1;
I hope it works out for you, I am sorry if I wasn't any help..
SELECT house, street, city, state, country, zip, rating
FROM table WHERE rating = (SELECT MAX(rating)
from table WHERE name = "Phills Coffee")
AND name= "Phills Coffee";
This should return:
4568 Sepulveda Blvd Torrance CA 50833 USA 6
Note that you can omit rating from the first line of this query and it will return the address only (no 6, but will still select the max rating's info)

Get some sort of Top Ten list of one column from table

I'd like to get some sort of Top Ten list from a table a MySQL database.
Here's an example. Let's say I have this table:
ID Username Town
1 foo Munich
2 bar Kolding
3 herp Bordeaux
4 derp Bordeaux
5 test Cologne
6 bla Munich
7 blob Bordeaux
And now, I'd like to get the most common entries in 'Town', like this:
Bordeaux 3
Munich 2
Cologne 1
Kolding 1
What kind of query can do that?
SELECT Town, COUNT(*) AS cnt FROM my_table GROUP BY Town ORDER BY cnt DESC LIMIT 10
How about this.
SELECT Town, Count(1) as TownCount
FROM myTable
GROUP BY Town
ORDER BY TownCount DESC;
If you want only 10 records, use
SELECT Town, Count(1) as TownCount
FROM myTable
GROUP BY Town
ORDER BY TownCount DESC
LIMIT 10;
Try this -
SELECT town,
count(*)
FROM tablename
GROUP BY town;