'Way to group rows in a table based on multiple columns
I am trying to find out if there is a way to group rows in a table within a database based on the values from multiple columns. My specific problem is that I am trying to form groups of people based on multiple different criteria that would be entered by students such as predefined choices for hobbies, a set time availability range, and a set GPA range. Currently I am working in Microsoft Access. Ideally the priority would be the choices for hobbies, time availability, and the GPA. I had tried to organize the data using the following SQL statement:
SELECT * FROM Student ORDER BY Interest, TimeAvailable, GPA
I then realized that my logic behind the organization was wrong because I realized I was using the ORDER BY incorrectly. I wanted the subsequent criteria to then get ordered all over again if there weren't matches from the first one. I guess I had a brain fart haha. I was planning on iteratively looping through the data selecting the top 3 rows each time using the following:
Declare @Counter = Int
Set @Counter = 1
While Student.GroupID = null
Begin
Declare @I = Int
Set I = 1
WHILE I <=3
(Something that would set the GroupID to @Counter)
Sample Data:
| FirstName | GroupID | Interest | TimeAvailability | GPA | |
|---|---|---|---|---|---|
| tomb | Tom | null | Hiking | 1-10 hours | 3.0 |
| Bjon | Bob | null | Reading | 11-20 hours | 2.5 |
| Rburn | Rex | null | Hiking | 1-10 hours | 3.0 |
| Gched | Grace | null | Sports | 11-20 hours | 3.0 |
| EPhil | Eren | null | Hiking | 1-10 hours | 2.0 |
| Gnen | George | null | Sports | 11-20 hours | 2.5 |
| PBull | Pedro | null | Hiking | 11-20 hours | 2.5 |
| FredF | Fred | null | Reading | 1-10 hours | 2.0 |
| RegM | Reggie | null | Reading | 11-20 hours | 2.0 |
Desired Output:
| FirstName | GroupID | Interest | TimeAvailability | GPA | |
|---|---|---|---|---|---|
| tomb | Tom | 1 | Hiking | 1-10 hours | 3.0 |
| Rburn | Rex | 1 | Hiking | 1-10 hours | 3.0 |
| EPhil | Eren | 1 | Hiking | 1-10 hours | 2.0 |
| Bjon | Bob | 2 | Reading | 11-20 hours | 2.5 |
| FredF | Fred | 2 | Reading | 1-10 hours | 2.0 |
| RegM | Reggie | 2 | Reading | 11-20 hours | 2.0 |
| Gched | Grace | 3 | Sports | 11-20 hours | 3.0 |
| Gnen | George | 3 | Sports | 11-20 hours | 2.5 |
| PBull | Pedro | 3 | Hiking | 11-20 hours | 2.5 |
To clarify, my goal would be to sort the data into groups of a specified integer (I had been using 3, but it could be more or less) based on the interest first and if there wasn't a match, it would go to the TimeAvailable, and if not a match, it would just keep going to GPA. In the above desired output, The interests are alike for the first two groups so that's why they were matched. Since there was one left over for the Hiking interest, PBull was grouped with Gched and Gnen due to TimeAvailability matching.
Any help is greatly appreciated. If there are better suggestions for trying to accomplish this please let me know!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
