SQL retrieving highest number from each group - mysql

I have a MySQL-table, containing these data:
idPK groupIDFK
1 1
2 1
3 2
4 2
5 1
I'm looking for a way to group the 2nd column values and display the highest value from the first column in that group. In this case, it should return these entries:
idPK groupIDFK
5 1
4 2
Does my description make sense? And if so, can someone please help me out?

This is your query where rows are sorted by groupIDFK:
select max(idPK) as idPK, groupIDFK from table group by groupIDFK;
If you need to order by idPK below is another version:
select max(idPK) as idPK, groupIDFK from table group by groupIDFK order by idPK desc;

Related

How to get exact rows count of particular column in MySQL table

I want to get exact row count of specified column
Example: Table
Name Id Age
_______________________________
Jon 1 30
Merry 2 40
William 50
David
There are 4 rows in table but i want to count ID column.
I am using below query to achieve it
select count(Id) from table;
But its returning 4 and I know why it is returning 4 but I want output as 2 because there are only two rows in Id column.
How can i achieve it?
Try this:
select count(Id) from table where id>0;
with the help of #blabla_bingo and #Edwin Dijk finally i have achieved it by below query
select count(Id) from table where Id!="";

Selecting a row with the same ID when the values of the second column are known

He's a beginner, and I've encountered this programming conundrum:
We have sample records in the table:
ID
VALUE
1
5
1
4
2
3
2
4
3
3
3
5
I would like to retrieve records with VALUE (3,4) values with the same ID, so as a result I would like to get ID 2
How to write such a mysql query to the database?
You can use HAVING.
SELECT id FROM table_name WHERE value IN (3, 4) GROUP BY id HAVING COUNT(*) = 2;
I have prepared an example for you to see how you work.

SQL: Repeated records by grouping some columns

I have a data like,
ID Name ItemA ItemB ItemC
OXZ234 Adam 4 4 5
OXZ234 Adam 1 2 3
OXZ345 Tarzen 6 7 8
OXDER2 William 9 8 2
OXDER2 William 0 8 0
I need to find how much of food each person eats. For example by referring first two records I can say, Adam of ID OXZ234 ate ItemA-5, ItemB-6 and ItemC-8. But for small amount of data this kind of manual calculation is affordable. I have a million data records like this. So initially I need to find the records which is having same ID and name but only items count differing.
I have tried the query to find duplicate records by grouping all columns like below,
select ID,Name,ItemA,ItemB,ItemC, COUNT(*)
from DATA_REFRESH
group by ID,Name,ItemA,ItemB,ItemC
having COUNT(*) > 1
But Now I have to identify records having items columns differed.
So the expected output is like,
OXZ234 Adam 2
OXDER2 William 2
OXZ345 Tarzen 1
Any suggestion would be helpful!
You want SUM
select ID,
Name,
sum(ItemA) as ItA,
sum(ItemB) as ItB,
sum(ItemC) as ItC,
count(ID) as Occurrences -- Counts the number of entries per person
from DATA_REFRESH
group by ID,Name
having count(ID) >1 -- restricts this so only those with more than one entry appear
Hi, You can have a simple query without having clause,
select ID,Name,COUNT(*)
from DATA_REFRESH
group by ID,Name order by COUNT(*) desc ;
Simply try like this,
select ID,Name,COUNT(*)
from Sample_Check
group by ID,Name
having COUNT(*) > 1

select records in given ids sorting order

i have table lets say - Students,
with 5 records and id(s) are 1 to 5, now i want to select the records - in a way that result should come like given sorting order of id column
id column should be resulted - 5,2,1,3,4
is there any other way to do this - then separate db calls for ids?
single db call ?
I guess if you really want a hard-coded order, you could do something like this:
order by case id
when 5 then 0
when 2 then 1
when 1 then 2
when 3 then 3
when 4 then 4
else 999
end
Or more simply (as #Strawberry points out in the comments):
order BY FIELD(id,4,3,1,2,5) desc

Selecting a Max Value based on another variable in SQL

I am trying to create an accurate count of student enrollment. There is an individual record for every course registration and any change to that particular course registration results in another record with an incremental sequence number. My table looks something like this:
ID Course Number Sequence Number
1 B101 1
1 B101 2
1 B101 3
1 C201 1
1 C201 2
2 E215 1
2 J320 1
2 J320 2
I would like to select the max value of sequence number such that every course registration is retained. This would mean that ID 1 would have 2 records. One would be B101 with sequence number=3 and another record for C201 with sequence number=2.
SELECT id, course_number, MAX(sequence_number) FROM table GROUP BY id, course_number;
You should first group all items on id then group them on Course_Number and show only the maximum value of Sequence_Number .
select id,Course_Number,max(Sequence_Number) from TblName group by id,Course_Number