Good Day,
I have a sql query that returns the following data:
EmployeeID...Employee
555..........John Doe
666..........Jane Doe
777..........Bob Smith
888..........Jane Smith
999..........Fred Jones
000..........Freda Jones
and I want my query to group by items of three:
Item.........EmployeeId........Employee
1............555...............John Doe
1............666...............Jane Doe
1............777...............Bob Smith
2............888...............Jane Smith
2............999...............Fred Jones
2............000...............Freda Jones
In other words, for every record that has more than 3 records, I want to increment Item by one every three records..
I am sure that I can use some sort of row insert into a table and keep track of the number of rows inserted into a table. But I’m trying to see if I can pull this off in SQL itself.
Is this possible?
TIA,
coson
If your database’s SQL variety supports ranking functions, you could try something like this:
Some database systems automatically perform integral division when both operands are integers, others support a dedicated operator (like
DIV) for integral division instead of/. In both mentioned casesFLOOR()would be unnecessary, of course.