user2444474
user2444474

Reputation: 573

How to do inner looping in java collection?

My SQL returns the values as like below.

enter code here

studentid   studentname   playid     gamename        grade        prizes   
---------  ------------   -----      ------------    ------      ---------   
121   bob                1          game1           A           1 and 2  
121   bob                2          game2           C           1 and 3  
121   bob                3          game3           B           4 and 2    
121   bob                4          game4           D           1 and 2  
131   jack               3          gam3            A           1    
131   jack               1          game1           A           2 and 3  

I'm getting the result and iterate the values to display.But in the last column need to display the values as different format.

Iterator<Search> iterator = products.iterator();   

while(iterator.hasNext())   
{   
Search req = (Search)iterator.next();   
req.getStudentid();   
req.getStudentname();   
req.getgameid();   
req.getgamename();   
req.getgrade();   
req.getprizes()          ;   
}  

Display format is...

studentid  studentname    playid    gamename     grade        prizes   
---------- -----------    ------    --------    -------      ---------   
121           bob         1         game1        A           1 and 2 and 3 and 4  
121           bob         2         game2        C           1 and 2 and 3 and 4  
121           bob         3         game3        B           1 and 2 and 3 and 4    
121           bob         4         game4        D           1 and 2 and 3 and 4   
131           jack        3         gam3         A           1 and 2 and 3    
131           jack        1         game1        A           1 and 2 and 3 

How to append prizes 4 rows values in first row ? How to loop through here ? Please help me.


EDIT: My SQL query is:

SELECT stu.studentid, stu.studentname,g.playid,stu.gamename,g.grade,g.prizes
  FROM student stu , game g 
  WHERE stu.studentid = g.studentid AND stu.year = g.year

Upvotes: 2

Views: 99

Answers (1)

sgeddes
sgeddes

Reputation: 62831

This can be done using just SQL, but I'm not sure it will have the best performance. This probably should be handled in your presentation logic. I would also suggest looking at normalizing your prizes column. Consider storing these a 1-n table (GamePrizes perhaps).

There are a few things you're trying to do. First, you want to combine all the prizes into a single value. You can use LISTAGG for that. However, it will not contain a distinct list. So to break up your list, you can use CONNECT BY and REGEXP_SUBSTR to split your list apart -- I'm using " and " as the delimiter in this case. Finally, put the distinct list of prizes back together using LISTAGG again, and you end up with something like this:

select stu.studentid, stu.studentname, 
  g.playid, g.gamename,g.grade,
  listagg(allprizes, ' and ') within group (order by allprizes) allprizes
from student stu 
  join game g on stu.studentid = g.studentid and stu.year = g.year
  join (
    select distinct studentid, regexp_substr(allprizes,'[^ and ]+', 1, level) allprizes
    from 
    (
      select studentid, listagg(prizes, ' and ') within group (order by prizes) allprizes
      from game
      group by studentid
    ) 
    connect by regexp_substr(allprizes, '[^ and ]+', 1, level) is not null
  ) p on g.studentid=p.studentid
group by stu.studentid, stu.studentname, 
  g.playid, g.gamename,g.grade

Resulting in:

STUDENTID   STUDENTNAME   PLAYID   GAMENAME   GRADE   ALLPRIZES
-------------------------------------------------------------------------
121         bob          1         game1      A       1 and 2 and 3 and 4
121         bob          2         game2      C       1 and 2 and 3 and 4
121         bob          3         game3      B       1 and 2 and 3 and 4
121         bob          4         game4      D       1 and 2 and 3 and 4
131         jack         1         game1      A       1 and 2 and 3
131         jack         3         game3      A       1 and 2 and 3

Upvotes: 1

Related Questions