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
Related
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 2 years ago.
I have the following query.
SELECT MAX(activity_id), group_id, parent_group_id FROM dashboard_activity_detail WHERE account_id = 8997 AND parent_group_id IN (5118,5026,4522,3983,3586,3278,3227) AND activity_type_id = 18 GROUP BY parent_group_id;
My expectation is that the group_id will be returned associated with the largest activity_id. There are multiple rows with the same parent_group_id in the table. What I get back is a value from a different row. The activity_id is correct and the parent_group_id is correct but I get a value from a different row for the group_id.
What am I missing? I've tried order by and various other methods with the same result.
You expectation is wrong. You query is malformed because the SELECT columns are inconsistent with the GROUP BY columns.
Use window functions (available in MySQL 8+):
SELECT da.*
FROM (SELECT da.*,
ROW_NUMBER() OVER (PARTITION BY parent_group_id ORDER BY activity_id DESC) as seqnum
FROM dashboard_activity_detail da
WHERE account_id = 8997 AND
parent_group_id IN (5118,5026,4522,3983,3586,3278,3227) AND
activity_type_id = 18
) da
WHERE seqnum = 1;
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
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
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
This question already has answers here:
Does COUNT(*) always return a result?
(7 answers)
Closed 9 years ago.
Hi all I have written a query to display the sum of quantity as follows without group by
SELECT ISNULL(SUM(VUItems.Quantity), 0) AS OrderQty
FROM VUItems
This returns as 0.00 but the same query when using group by not displaying 0.00 what might be the problem
SELECT ISNULL(SUM(VUItems.Quantity), 0) AS OrderQty
FROM VUItems
GROUP BY SKU,
SalesOrderNo
Why I need is that I will have a table which will have the quantity on saving this is what I have written to display the Quantity ordered or user eneterd as follows
SELECT VU1.*,
VU1.Quantity - (SELECT ISNULL(SUM(VU2.Quantity), 0) AS OrderQty
FROM VU2
WHERE VU1.SKU = VU2.SKU
AND VU1.SalesOrderNo = VU2.SalesOrderNo
GROUP BY SKU) AS orderedQuantity
FROM VU1
with out group by it is displaying orderedQuantity as required but with group by it is showing null
The ISNULL needs to be inside the SUM. SUM(NULL) is NULL.