Get the most often occurring year [duplicate] - mysql

This question already has answers here:
Find most frequent value in SQL column
(11 answers)
Closed 4 years ago.
I have the following table from a factory database called world in MySQL Workbench:
Table: country
Relevant Columns:
Code char(3) PK,
IndepYear smallint(6)
I'd like to get the year (or possibly years) when the most countries have become independent. I don't want to list anything else just the year(s), one row per year.
EDIT:
I have found the right solution that actually works.
SELECT IndepYear
FROM (SELECT IndepYear,
COUNT(IndepYear) AS Years
FROM world.country
GROUP BY IndepYear
ORDER BY Years DESC) AS T1
WHERE Years IN (SELECT Years
FROM (SELECT IndepYear,
COUNT(IndepYear) AS Years
FROM world.country
GROUP BY IndepYear
ORDER BY Years DESC
LIMIT 1) AS T2
GROUP BY IndepYear)
ORDER BY IndepYear ASC;

It sounds like you just want to ORDER BY the COUNT of IndepYear. Don't forget that you'll also want to GROUP BY this column, along with adding a LIMIT of 1 to get the most frequently occuring year when sorting it in descending order.
SELECT `IndepYear`,
COUNT(`IndepYear`) AS `CommonYear`
FROM `country`
GROUP BY `IndepYear`
ORDER BY `CommonYear` DESC
LIMIT 1;

Use aggregation to get the count of each year and limit it to the maximum count in a HAVING clause. Use a subquery to get the maximum count, aggregating the aggregation.
SELECT indepyear
FROM country
GROUP BY indepyear
HAVING count(*) = (SELECT max(c)
FROM (SELECT count(*) c
FROM country
GROUP BY indepyear) x);
SQL Fiddle

Related

Get Top 5 latest records for different IDs in SQL [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 2 months ago.
I have a table named Work_Items like this:
Assume there are lots of Names (i.e., E,F,G,H,I etc.,) and their respective Date and Produced Items in this table. It's a massive table, so I'd want to write an optimised query.
In this, I want to query the latest A,B,C,D records.
I was using the following query:
SELECT * FROM Work_Items WHERE Name IN ('A','B','C','D') ORDER BY Date DESC OFFSET 0 LIMIT 4
But the problem with this query is, since I'm ordering by Date, the latest 4 records I'm getting are:
I want to get this result:
Please help me in modifying the query. Thanks.
On MySQL 8+, we can use ROW_NUMBER:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Date DESC) rn
FROM Work_Items
WHERE Name IN ('A', 'B', 'C', 'D')
)
SELECT Name, Date, ProducedItems
FROM cte
WHERE rn = 1
ORDER BY Name;
You can use inner join as follows, its working on any mysql version:
select w.name, w.`date`, w.ProducedItems
from _Work_Items w
inner join (
select name, max(date) as `date`
from _Work_Items
group by name
) as s on s.name = w.name and s.`date` = w.`date` ;

Using Order by And Group By Together Eliminates some Records [duplicate]

This question already has answers here:
MySQL Group by ID and Latest Datetime
(4 answers)
MySql : Order by and Group By combining not giving the latest record
(2 answers)
MySQL: Multiple Group By With Desc Order Issue
(1 answer)
Closed 3 years ago.
user_id date
------- ---------
1 1551867583
2 1551867580
3 1551867573
2 1551867543
2 1551867521
I have a similar table structure. What I want to do is to select records per user and select the latest one of each user's record.
Tried
GROUP BY profile_visits.user_id ORDER BY profile_visits.date DESC
This doesn't show all the latest records.
I searched and found this on StackOverflow
group by and order by in mysql query
However, that solution didn't work for me
Any idea how to solve this?
You can use max date and group by
select max(date) max_date, user_id
from my_table
group by user_id
order by user_id, max_date

getting the lowest count value in mysql [duplicate]

This question already has answers here:
Mysql - Get row with lowest relation count
(2 answers)
Closed 4 years ago.
I just want the most least repeated count like in the image 1 only to be displayed. But i am having problem
SELECT bidprice, user_id, COUNT(*)
FROM auction_details WHERE product_id = '1'
GROUP BY bidprice
if you need only the lower count result you could using limit 1
SELECT bidprice, user_id, COUNT(*) my_count
FROM auction_details WHERE product_id = '1'
GROUP BY bidprice
order by my_count ASC limit 1

Use COUNT(), ORDER BY and WHERE user = ? simultaneously [duplicate]

This question already has answers here:
Rank function in MySQL
(13 answers)
Closed 7 years ago.
I have a table with highscores. When I read them I order them by score DESC.
scores
id name score
i.e.
SELECT name, score FROM scores ORDER BY score DESC
Now I would like to know the rank of a person. I am trying to find a way to combine this without having to loop through all the highscores. This is what I thought of, but I know this will not work. Any ideas?
SELECT COUNT(id), name, score FROM scores WHERE name = ? ORDER BY score DESC
Should I use WHERE?
You could count everyone with a higher score in a subquery:
select coalesce((select count(1) from scores b where b.score > a.score),0) + 1 Rank
, Name
, Score
from Scores a
where name = 'Sarah'
SQL Fiddle: http://sqlfiddle.com/#!9/ff0133/3

Limit SQL Results to one result per pkey searched using "in" [duplicate]

This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 8 years ago.
I have a history table which I would like to use to find the latest user in which updated specific items. Here is the query I have so far:
SELECT *
FROM `history`
WHERE `pKey`
IN ( 13309, 13311, 13951, 14244, 1500, 15558, 15691, 15938, 9769 )
ORDER BY `history`.`time` DESC
LIMIT 0 , 30
This returns multiple history results for each pkey. Is there a way to limit the results to only the latest (based on time) entry from the specific pkey?
So for example:
Right now pkey 13309 has multiple results returned. The query should only return the latest result for it. Same goes for 13311... etc.
This should do:
SELECT h.*
FROM `history` as h
INNER JOIN (SELECT `pkey`, MAX(`time`) as MaxTime
FROM `history`
WHERE `pkey` IN (13309, 13311, 13951, 14244, 1500,
15558, 15691, 15938, 9769)
GROUP BY `pkey`) as t
ON h.`pkey` = t.`pkey`
AND h.`time` = t.`MaxTime`
this should work. Just grouping all the rows that have the same pkey. I think this will work. Comment with a feedback.
Select * from (
SELECT *
FROM `history`
WHERE `pKey`
IN ( 13309, 13311, 13951, 14244, 1500, 15558, 15691, 15938, 9769 )
ORDER BY `history`.`time` DESC) as t1 group by pKey