Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:01:42+00:00 2026-05-31T17:01:42+00:00

I have a table with below structure. I am not having control over changing

  • 0

I have a table with below structure.

I am not having control over changing the table.

It has three columns: student_name, student_id, name_id

Now student name could be any number of words. Exactly one word will come in one row. Based on the number of words, name_id will be entered and the student_id will be repeated.

The structure would be some thing like:

say name1 is: Ram Laxman Prasad Sharma

and name2 is: Pandit Gangadhar Vidyadhar Mayadhar Omkarnath Shastri

So the table will look like:

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 explained the structure clearly.

Now, I want to write a query to read only first four names per student. However, if the number of names is less then four, empty string should go and if its greater then four, first four should go and rest just ignored.

I need to put it in a single select query only, since this query will be passed in a spring batch program. But I am not getting how to loop through nameid column to take first four name id for every student.

How to design this sql for DB2 database v8??

Thanks for reading.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:01:44+00:00Added an answer on May 31, 2026 at 5:01 pm

    improved version inspired by Amit – if you need all 4 names in 1 column 🙂

      select
        t1.student_name || 
        coalesce(' ' || t2.student_name, '') ||
        coalesce(' ' || t3.student_name, '') ||
        coalesce(' ' || t4.student_name, '') as "first 4 names"
      from mytable t1
      left join mytable t2 on t1.student_id = t2.student_id and t2.name_id = 2
      left join mytable t3 on t1.student_id = t3.student_id and t3.name_id = 3
      left join mytable t4 on t1.student_id = t4.student_id and t4.name_id = 4
      where t1.name_id = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a mysql database with a table structure like below : Table Name
I have a table T (structure below) which initially contains all-NULL values in an
I have a table structure like below : categoryID bigint , primary key ,
I have a organization name table with the following structure given below: CREATE TABLE
i have a table with 3 columns. It is having duplicate on column 2
i have table with below structure: <table class=cedvel> <caption> count: <%:Html.Encode(TempData[RowsCount])%></caption> <thead> <tr> <th
I have table structure like below: events (boxing, sparring etc) competitors (users who are
Assume that I have a table structure like seen below location|name ------------- NY|john London|neil
I have a below table structure for friends table TABLE STRUCTURE CREATE TABLE `my_friends`

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.