1

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
jomacm04
  • 11
  • 5

2 Answers2

2
SELECT id, course_number, MAX(sequence_number) FROM table GROUP BY id, course_number;
Vander Rice
  • 161
  • 1
  • 10
  • Thank you very much! I have very limited SQL sqills (see what I did there), but am trying to teach myself. Do you have any recommendations on where to start? – jomacm04 Oct 13 '14 at 19:39
  • My experience has come from several database backed web applications and a SQL intensive job. SQL has been around a while, there are many good books on the topic - Amazon would have many suited for level of experience. Knowledge of discrete math and relational algebra would be helpful as well. I'd recommend finding a data set or database of data in a topic you're interested in, like healthcare or weather data, and experimenting to find interesting things about the data. Just keep writing SQL and you'll have SQills in no time. – Vander Rice Oct 13 '14 at 19:48
0

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 
Arash
  • 1,692
  • 5
  • 21
  • 36
  • 1
    Can you please add some explanation regarding why and how this would be an answer the OP's question? – Ryan Gates Oct 13 '14 at 19:42
  • I grouped items on ID first and then grouped on Course_Number and it shows only maximum amount of Sequence_Number . – Arash Oct 13 '14 at 19:47
  • Don't add explanation as a reply to my comment! Edit your answer to make it better, more meaningful and complete. – Ryan Gates Oct 13 '14 at 21:16