This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Get top n records for each group of grouped results
(12 answers)
Closed 3 months ago.
I have created a table that has category id and a name and the table contains multiple matching category id so i would like to get the first data of each matching category id
based on the example table above i would like to get just the name alex and brown
Here is what i have tried
SELECT * FROM tailors
WHERE id IN(
SELECT min(id)
FROM tailors
GROUP BY cat_id,id,name,status
)
but i am getting all the record when i am just trying to get the first data of each matching category id
You just need to take out id and name from your group by clause -
SELECT * FROM tailors
WHERE id IN (SELECT min(id)
FROM tailors
GROUP BY cat_id, status
);
If the logic remains same throughout the table, and the version of the DB is 8.0+, then use such an aggregation :
SELECT name
FROM( SELECT t.*, MIN(id) OVER (PARTITION BY cat_id) AS min
FROM tailors AS t ) AS tt
WHERE id = min
assuming id is a primary key column, there will be only one minimum value per each cat_id.
GROUP BY cat_id is handled by PARTITION BY cat_id and the other columns(name and status) following it should be removed.
To return only one row use LIMIT 1:
SELECT * FROM tailors
WHERE id IN(
SELECT min(id)
FROM tailors
GROUP BY cat_id,id,name,status
) LIMIT 1
Related
This question already has answers here:
Get records with max value for each group of grouped SQL results
(19 answers)
Closed last month.
i want to select row by the highest votecount from a table, but if the userid is repeted i want the just the row with the highest votecount. eg:
userId
votecount
2
2
2
12
1
20
my result is supposed to be:
1,20
2,12
this is my current code:
SELECT * from `audio` GROUP BY `userId` ORDER BY `votecount` DESC LIMIT 50
the result of my code is :
1,20
2,2
it's grouping by userId and then ordering it by votecount, which is not the desired output
select `userid`, MAX(`votecount`)
FROM `audio`
GROUP BY `userId` ORDER BY `votecount`
If that's the case and you don't have a unique row id, you need to add the other columns in the group by clause
SELECT date, promoted, status, image, name, id, userId, MAX(votecount)
FROM audio
GROUP BY date, promoted, status, image, name, id, userId
ORDER BY votecount DESC
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` ;
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
How can I SELECT rows with MAX(Column value), PARTITION by another column in MYSQL?
(22 answers)
MySQL - How to select rows with max value of a field
(3 answers)
Closed 1 year ago.
I'm looking for a way to display a column in my table without aggregating it (column store)
SELECT id, name, max(cost), store
FROM test
group by 1,2,4
But in the table im using there are some row that shares the same id,name,license with different machine, and Ideally I'd like to get in return only a single row, with max(cost), if they share the same first 2 columns and only display the last column - without using it to group by.
actual outcome:
id
name
max(cost)
store
1
Joe
30
store1
1
Joe
50
store2
but my desired result will be:
id
name
max(cost)
store
1
Joe
50
store2
Can it even be done?
You seem to want the row with the maximum cost. A typical method uses row_number();
select t.*
from (select t.*,
row_number() over (partition by id, name order by cost desc) as seqnum
from test t
) t
where seqnum = 1;
No aggregation is needed.
You can also use a correlated subquery:
select t.*
from test t
where t.cost = (select max(t2.cost)
from test t2
where t2.id = t.id
);
This question already has answers here:
How to count occurrences of a column value efficiently in SQL?
(7 answers)
Closed 5 years ago.
I have got a table with ID's:
Id
===
1
2
2
3
1
2
And I want to create a table with two columns like this:
Id COUNT
=============
1 2
2 3
3 1
How can I do that?
Let's say you called your table 'user', you can try this :
SELECT user.Id as ID, count(user.Id) as COUNT_ID
FROM user
GROUP BY ID;
Hope it helps,
WaLinke
You have to group by your id.
Select id, count(1) as COUNT
from yourtable
group by ID
order by id
That way you tell your sql, that you want to count the number of rows per id.
If you need more examples feel free to google sql count.
There are many good examples out there.
Or check this stackoverflow question:
How to use count and group by at the same select statement
You can use GROUP BY and Count(columnname) functions like this
SELECT
Id,
Count(Id) AS COUNT
FROM
tablename
GROUP BY Id
Something like this should work
SELECT id, COUNT(id) AS Expr1 FROM dbo.Table1 GROUP BY id
I have a table in a MySQL database with an ID column. This is not a key of the table and several rows can have the same ID.
I don't really know SQL but I already figured out how to obtain the number of distinct IDs:
SELECT COUNT(DISTINCT ID) FROM mytable;
Now I want to count only those IDs which appear more than 2 times in the table.
So if the ID column contains the values
3 4 4 5 5 5 6 7 7 7
the query should return 2.
I have no idea how to do this. I hope someone can help me!
Btw, my table contains a huge number of rows. So if there are several possibilities I would also be happy to know which solution is the most efficient.
Try this:
SELECT COUNT(ID) FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING COUNT(ID) > 2) p
select count(*) from
(select count(id) as cnt,id from mytable group by id) da
where da.cnt>2
The inner query will give you how many elements does each id have. And the outer query will filter this.
SELECT
COUNT(ids)
FROM
(SELECT
COUNT(ID)AS ids
FROM
mytable
GROUP BY
ID
HAVING
ids>2
)AS tbl1
Updated :
SELECT count(ID)
FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING count(ID) > 2
) p
should do what you need