Hi I want to display the country(Land in german) which haves the most museums.
My table looks like:
+-----------+----------------------+-------------------+--------------------------+---------------+
| MuseumsNR | Name | Stadt | Land | Hauptstadt |
+-----------+----------------------+-------------------+--------------------------+---------------+
| 1 | Museum of Modern Art | New York | United States of America | Washington DC |
| 2 | Kunstmuseum | Bern | Schweiz | Bern |
| 3 | Musée Picasso | Paris | Frankreich | Paris |
| 4 | Städel | Frankfurt am Main | Deutschland | Berlin |
| 5 | Museum Ludwig | Köln | Deutschland | Berlin |
+-----------+----------------------+-------------------+--------------------------+---------------+
So my desired output should be:
+--------------------------+------+
| Land |Anzahl|
+--------------------------+------+
| Deutschland | 2 |
+--------------------------+------+
This is what i´ve tried:
SELECT Land, COUNT(Name) Name from Museum order by Name desc;
THe Output:
+--------------------------+------+
| Land | Name |
+--------------------------+------+
| United States of America | 5 |
+--------------------------+------+
Thanks in advance for your help!
SELECT Land, count(*) as Anzahl
FROM yourTable
GROUP BY Land
ORDER BY Anzahl
DESC LIMIT 1;`
Only tested in PostgreSQL, but should be close enough in MySQL.
Related
I'm not really sure how to ask this and I've been search-engining for awhile and haven't come up with anything useful.
Say I have the following three tables People, Cities and States:
PeopleID | Name | Age | CityIDFK
--------------------------------------------
1 | John | 24 | 20
2 | Jim | 28 | 21
3 | Joan | 49 | 10
4 | Mike | 37 | 10
5 | Bruce | 26 | 2
6 | Peter | 22 | 20
7 | Oprah | 27 | 3
7 | Jake | 21 | 1
CityIDPK | City | StateIDFK
---------------------------------------
1 | Seattle | 1
2 | Gotham | 2
3 | Oakland | 4
10 | Boise | 5
20 | Austin | 6
21 | Tyler | 6
StateIDPK | StateName
----------------------------
1 | Washington
2 | New York
3 | Oregon
4 | California
5 | Idaho
6 | Texas
How can I achieve the following output:
StateName | City | Name
---------------------------------------
California | Oakland | Oprah
Idaho | Boise | Mike
| | Joan
New York | Gotham | Bruce
Washington | Seattle | Jake
Texas | Austin | John
| | Peter
| Tyler | Jim
I'm not sure if the above output is possible or not, I would probably just do something like this:
SELECT StateName, City, Name FROM People
INNER JOIN Cities
ON Cities.CityIDPK = People.CityIDFK
INNER JOIN States
ON States.StateIDPK = Cities.StateIDFK
But that would return the StateName and City for every person, instead of just once.
StateName | City | Name
---------------------------------------
California | Oakland | Oprah
Idaho | Boise | Mike
Idaho | Boise | Joan
New York | Gotham | Bruce
Washington | Seattle | Jake
Texas | Austin | John
Texas | Austin | Peter
Texas | Tyler | Jim
If the output I want to achieve is possible, would someone show me an example of how to write the query, or point me in the right direction?
I agree with Tim, this should be handled in your presentation layer.
But you can do it if you're using MySQL 8 by taking advantage of window functions. We can use row_number() to determine the first row per state and per city, then use a case to only display the name for the first row.
SELECT
case row_number() over states_w
when 1 then states.name
else '' end as StateName,
case row_number() over cities_w
when 1 then cities.name
else '' end as CityName,
people.name as Name
FROM People
INNER JOIN Cities ON Cities.id = People.City_id
INNER JOIN States ON States.id = Cities.state_id
window states_w as (partition by states.id),
cities_w as (partition by cities.id);
If you're using MySQL before 8... upgrade. If you can't upgrade, it can be emulated.
I've got data with geographical locations. That may be cities, provinces, states, countries, continents, or whatever kind of location. A location can be part of an other location, like a State is part of a Country (like the US), but The Netherlands has provinces. Cities can be in a Province but they aren't necessarily part of a province. Partly because some cities are city-state(-like) (i.e. Luxembourg), but in my case it's not even relevant in what province a city is, if there's only one city from a country in my locations list.
This this very simple example:
| types |
|-----------|
| Continent |
| Country |
| Province |
| City |
Locations
| id | name | type | parent |
|----|---------------|-----------|--------|
| 1 | Europe | Continent | NULL |
| 2 | Netherlands | Country | 1 |
| 3 | Noord-Holland | Province | 2 |
| 4 | Amsterdam | City | 3 |
| 5 | Haarlem | City | 3 |
| 6 | Luxembourg | City | 1 |
For every location I want to know their 'geographical parent' (if exist). So the expected outcome is this:
| id | name | type | Continent | Country | Province |
|----|---------------|-----------|-----------|-------------|---------------|
| 1 | Europe | Continent | | | |
| 2 | Netherlands | Country | Europe | | |
| 3 | Noord-Holland | Province | Europe | Netherlands | |
| 4 | Amsterdam | City | Europe | Netherlands | Noord-Holland |
| 5 | Haarlem | City | Europe | Netherlands | Noord-Holland |
| 6 | Luxembourg | City | Europe | | |
How can I get all types as a column for my Locations table? I've tried to use subqueries, but I'm completely stuck because of the recursiveness: Amsterdam is not part of a country (but it's Province is), while Luxembourg is part of a country (without a Province).
How can I get the expected output?
Surely there is a better solution but at the moment I wrote this:
SELECT Locations.id,
Locations.name,
Locations.type,
IF(l3.name IS NULL, IF(l2.name IS NULL, IFNULL(l1.name, ''), l2.name), l3.name) as Continent,
IF(l3.name IS NULL, IF(l2.name IS NULL, '', l1.name), l2.name) as Country,
IF(l3.name IS NULL, '', l1.name) as Province
FROM Locations
LEFT join Locations l1 on Locations.parent = l1.id
LEFT join Locations l2 on l1.parent = l2.id
LEFT join Locations l3 on l2.parent = l3.id
ORDER BY Locations.id
DEMO
I have two sql tables that I am trying to join together. I'm trying to make it so that a userid from my first table- table "video" and the posted_by from table "review" are joined so that the output lists a user pair where that they always gave each other "five” for the "star" column
I was working with this statement, but it's not yielding any results.
There are only two users in the tables that fit the requirement: Jstark#gmail.com and cmanley#gmail.com
Query:
SELECT U1.userid, U2.userid
FROM video U1, video U2
WHERE U1.userid < U2.userid
(SELECT COUNT(*)
FROM video
WHERE userid = U1.userid) =
(SELECT COUNT(J.star) FROM video J, review R
WHERE J.userid = U1.userid AND J.star = R.star AND R.posted_by = U2.userid AND R.star = "5")
review
(SELECT COUNT(*)
FROM video
WHERE userid = U2.userid) =
(SELECT COUNT(J1.star
FROM video J1, review R1
WHERE J1.userid = U2.userid AND J1.star = R1.star AND R1.posted_by = U1.userid AND R1.star = "5");
Table video:
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| video_url | video_name | video_description | video_subject | video_city | video_tags | star | date | userid | reviewed_by |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://www.youtube.com/watch?v=xGa9AbKO-4s | Nice video of Detroit | Watch as someone shows you Detroit | Downtown Detroit | Detroit | lake | 5 | 2019-11-29 | atticus#yahoo.com | Michael#aol.com |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/5KE7ppUIPhQ | Sacaomento Forest | A nice forest in Sacromento | Forest in Sacramento | Sacramento | lake | 3 | 2019-11-30 | Don#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/5KE7ppUIPhQ1 | Sacromento Forest | A nice forest in Sacromento | Forest in Sacromento | Sacromento | lake | 5 | 2019-12-01 | Don#yahoo.com | |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/BnbE6QzKY-I | Great video of downtown Dallas | See a tour of downtown Dallas | Downtown Dallas | Dallas | lake | 0 | 2019-12-01 | john#gmail.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/HGvwO8qq8FI | Tour of Cape Cod | See a tour of Cape Code | View of Cape Code | Cape Code | lake | 5 | 2019-12-02 | Michael#aol.com | atticus#yahoo.com |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/HiZXABMNCUY | Tour of Manhattan | See Manhattan on the big screen | Downtown New york | Manhattan | lake | 2 | 2019-11-27 | Don#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/ORoyGEgvfXY | Nice video of Orlando | See the appeals of Orlando | Downtown Orlando | Orlando | lake | 4 | 2019-11-28 | Hhinata#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/pKwuW06NvXM | Scenic Downtown Cincinatti | Take the backroutes through Cincinatti | Take the backroutes through Cincinatties | Cincinatti | lake | 3 | 2019-11-26 | Jstark#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/QSwvg9Rv2EI | Tour of Chicago | Let us bring Chicago to you | Downtown Chicago | Chicago | lake | 2 | 2019-11-25 | john#gmail.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/qY02yEvqFio | Downtown Detroit | Tour of Downtown Detroit | Downtown Detroit | Detroit | lake | 5 | 2019-11-27 | Megan#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/_QH_tdYR3iI | Great video of Lake Erie | See the great lakes up close | Michigan great lakes | Traverse City | lake | 0 | 2019-11-27 | cmanley#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
Table review:
+--------------------------------+--------------------------+-------------+-------------------+
| video_name | review_comments | review_star | posted_by |
+--------------------------------+--------------------------+-------------+-------------------+
| Nice video of Detroit | cool dude | 2 | john#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Tour of Chicago | cool video | 5 | Jstark#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Tour of Cape Code | could have been better | 3 | Megan#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Great video of downtown Dallas | good video | 5 | Don#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Great video of downtown Dallas | great video | 0 | Don#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Nice video of Detroit | great video! | 2 | don#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Great video of Lake Erie | i really didnt like this | 0 | cmanley#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Great video of downtown Dallas | it was terrible | 0 | Don#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Great video of downtown Dallas | Nice van | 4 | Hhinata#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Nice video of Detroit | nice video | 5 | test#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Sacramento Forest | okay video | 3 | atticus#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Scenic Downtown Cincinatti | terrible video | 0 | Michael#aol.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Nice video of Orlando | wonderful video | 5 | Hhinata#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+
To solve the problem I'm going to start at some basic queries and work towards the answer asked.
Start by collating the the videos and the reviews with a join
SELECT v.video_name, AVG(review_star)
FROM video v
JOIN review r
USING (video_name)
GROUP BY v.video_name
Then we can look at the average star rating for reviewers:
SELECT r.posted_by, AVG(review_star)
FROM review r
GROUP BY r.posted_by
Take a step further and group by the poster/review pair:
SELECT v.userid, r.posted_by, AVG(review_star)
FROM video v
JOIN review r
USING (video_name)
GROUP BY v.userid, r.posted_by
HAVING allows a post filter on this. All 5 star rating will have an average of 5.
SELECT v.userid, r.posted_by, AVG(review_star) as avg
FROM video v
JOIN review r
USING (video_name)
GROUP BY v.userid, r.posted_by
HAVING avg = 5
I am using the wrong syntax. It's confusing as a beginner to get mixed up with using the comma, quotation and the tic mark.
-list the form of government for the countries with the top 5 average GNP.
Here is the statement that I am using and the results.
SELECT 'avg-gnp','form-government'
FROM country
ORDER BY 'avg-gnp'
DESC LIMIT 5;
+---------+-----------------+
| avg-gnp | form-government |
+---------+-----------------+
| avg-gnp | form-government |
| avg-gnp | form-government |
| avg-gnp | form-government |
| avg-gnp | form-government |
| avg-gnp | form-government |
+---------+-----------------+
5 rows in set (0.01 sec)
I've also tried this: Thanks in advance!
mysql> SELECT * FROM country ORDER BY 'avg-GNP' DESC LIMIT 5;
+------+-------------+---------------+---------------------------+--------+-------------------+------------+--------------+---------+----------------------------------------------+
| code | fullname | continent | region | area | year-independence | population | avg-lifespan | avg-GNP | form-government |
+------+-------------+---------------+---------------------------+--------+-------------------+------------+--------------+---------+----------------------------------------------+
| ABW | Aruba | North America | Caribbean | 193 | 0 | 103000 | 78.40 | 828.00 | Nonmetropolitan Territory of The Netherlands |
| AFG | Afghanistan | Asia | Southern and Central Asia | 652090 | 1919 | 22720000 | 45.90 | 5976.00 | Islamic Emirate |
| AGO | Angola | Africa | Central Africa | 124670 | 1975 | 12878000 | 38.30 | 6648.00 | Republic |
| AIA | Anguilla | North America | Caribbean | 96 | 0 | 8000 | 76.10 | 63.20 | Dependent Territory of the UK |
| ALB | Albania | Europe | Southern Europe | 28748 | 1912 | 3401200 | 71.60 | 3205.00 | Republic |
+------+-------------+---------------+---------------------------+--------+-------------------+------------+--------------+---------+----------------------------------------------+
5 rows in set (0.00 sec)
This is a comment that cannot be put in a comment since comments don't allow back ticks. I wanted to make sure you got it right.
Correct Form (this is how it should look using back ticks):
SELECT `avg-gnp`,`form-government`
FROM country
ORDER BY `avg-gnp`
DESC LIMIT 5;
Wrong Form (this is the way you had it using single quotes):
SELECT 'avg-gnp','form-government'
FROM country
ORDER BY 'avg-gnp'
DESC LIMIT 5;
Do you see the difference?
And yes, MySQL is weird about this. The only database I know that uses back ticks (well... and MariaDB, of course).
I have a table which has data in the following format:
+---------------------+--------------+-------------------+-------------------+
| date | downloadtime | clientcountrycode | clientcountryname |
+---------------------+--------------+-------------------+-------------------+
| 2013-07-10 10:44:29 | 2 | USA | United States |
| 2013-07-10 10:44:25 | 4 | USA | United States |
| 2013-07-10 10:44:21 | 7 | USA | United States |
| 2013-07-10 10:44:16 | 2 | USA | United States |
| 2013-07-10 10:44:10 | 3 | USA | United States |
+---------------------+--------------+-------------------+-------------------+
I need to prepare a csv file by querying this table. The csv file should be of the following format:
clientcountryname,clientcountrycode,2013-07-05,2013-07-06,2013-07-8...
United States,USA,22,23,24
SO, basically I need to get the average downloadtime for each country for each day.
I have a query which will give me avg(downloadtime) for a particular day:
SELECT clientcountryname,clientcountrycode, avg(downloadtime), FROM tb_npp where date(date) = '2013-07-10' group by clientcountrycode;
+---------------------------------------+-------------------+-------------------+
| clientcountryname | clientcountrycode | avg(downloadtime) |
+---------------------------------------+-------------------+-------------------+
| Anonymous Proxy | A1 | 118.0833 |
| Satellite Provider | A2 | 978.5000 |
| Aruba | ABW | 31.8462 |
My question is: Is there a way in SQL to group the column names based on date which is present in my database?
If I understand you question correctly, you should just be able to group by the date as well:
SELECT clientcountryname,clientcountrycode,Date, avg(downloadtime),
FROM tb_npp
GROUP BY clientcountrycode,clientCountryCode,Date;