There is a label with the fields id - PK AI, event - the name of the event, year - the year in which the event occurred:
events: id | event | year 1 | X | 2000 2 | X | 2000 3 | Y | 2000 4 | Z | 2001 6 | T | 2002 1) It is necessary to bring the most popular events in the most popular year. Those. in this case, the most popular year is 2000, because 3 events happened in it at once. And the most popular event in 2000 is X.
The result should be in the form: event, number, year .
event | cnt | year X | 2 | 2000 2) If there are several most popular events, then you need to display all the most popular events in the most popular year. For example for:
id | event | year 1 | X | 2000 3 | Y | 2000 4 | Z | 2001 6 | T | 2002 The sql query should return 2 rows:
event | cnt | year X | 1 | 2000 Y | 1 | 2000 3) Write a sql query that would display the most popular events in the most popular years. Those. may be some of the most popular years. For example:
id | event | year 1 | X | 2000 3 | Y | 2000 4 | Z | 2001 6 | T | 2001 7 | W | 2005 The result should be:
event | cnt | year X | 1 | 2000 Y | 1 | 2000 Z | 1 | 2001 T | 1 | 2001 1) My version is that the first query can be written as (it does not work correctly on all data):
select event, count(*) cnt, year from events group by year, event order by cnt desc limit 1; - How to fix query for option 1?
- How to write a sql query correctly so that it works for options 2 and 3?