I have a table with below structure.
I have no control over table changes.
It has three columns: student_name, student_id, name_id
Now the studentβs name can be any number of words. Exactly one word will appear in one row. Based on the number of words, id_name will be entered, and student_id will be repeated.
The structure should be something like:
say name1: Ram Lakshman Prasad Sharma
and name2: Pandit Gangadhar Vidyadhar Mayadhar Omkarnath Shastri
So the table will look like this:
student_name | student_id | name_id ------------------------------------------------- Ram 1 1 Laxman 1 2 Prasad 1 3 Sharma 1 4 Pandit 2 1 Gangadhar 2 2 Vidyadhar 2 3 Mayadhar 2 4 Omkarnath 2 5 Shastri 2 6
I hope I clearly explained the structure.
Now I want to write a query to read only the first four names for each student. However, if the number of names is less than four, an empty line must go, and if it is more than four, the first four must go, and the rest are simply ignored.
I need to put it in only one select request, since this request will be passed in the spring batch program. But I don't get how to quote the table of the column nameid to accept the first four name identifiers for each student.
How to create this sql for DB2 v8 database ??
Thank you for reading.