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
Related
select * from "Test"."EMP"
id
1
2
3
4
5
Select SUM(1) FROM "Test"."EMP";
Select SUM(2) FROM "Test"."EMP";
Select SUM(3) FROM "Test"."EMP";
why the output of these queries is?
5
10
15
And
I don't understand why they write table name like this "Test"."EMP"
your table has 5 records. the statement select 1 from test.emp returns 5 records with values as 1 for all 5 records.
id
1
1
1
1
1
This is because db engine simply returns 1 for each existing record without reading the contents of the cell. and same happens for select <any static value> from test.emp
same happens for 2 and 3
id
2
2
2
2
2
hence there are 5 records returned with the static values and sum of those values will be the product of static number passed in the select statement and total records in the table
additional fact: It is always recommended to perform count(1) than count(*) as it consumes less resource and hence less load on the server
I don't think it's "Test"."EMP" with double quotes.. it's probably `Test`.`EMP` with backticks instead. The definition means its database_name.table_name. This is the recommended format to get the correct table_name from database_name; in this case, you're specifically making the syntax to query from `Test`.`EMP`. Read more about identifier qualifiers.
As for SUM(x), the x get's repeated according to the rows present in the table. So SUM(1) on 5 rows is 1+1+1+1+1, SUM(2) on 5 rows is 2+2+2+2+2, and so on.
hello I have an as3 app. this app divides the users into groups, every group consisting of 3 users.
in my mysql database there is a field in "users_into" table that identifies the number of user in his group.
this field is called "num_in_group" and its value must be a number between 1 and 3 for every user.
For clarification
The first user who registered in the application will have number 1 and the second one will have number 2 and the third one will have number 3 ---and the forth one will have 1 (not 4) ----- and fifth one will have 2 and sixth one will have 3 and seventh one will have 1 again and so on ......
so my question is how can I make the field have numbers 1 , 2 , 3 , 1 , 2 , 3 in the order constantly
One option which you might find acceptable would be to use ROW_NUMBER() and generate this value at the time you query:
SELECT
id,
1 + (ROW_NUMBER() OVER (ORDER BY id) - 1) % 3 AS seq
FROM yourTable
ORDER BY id;
The above assumes that id is an auto increment column, with a unique idea for each user. The only potential problem with this approach is that the id might not always be sequential or increasing. Assuming you can tolerate the vast majority of users have an even spread of 1, 2, and 3 values, then the above might be acceptable to you.
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
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;
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