What to do when the tabulation method in the table and the tabulation method displayed on the screen are different in Java?

Asked 1 years ago, Updated 1 years ago, 424 views

Hello.

I'm designing a Java interface for my business.
I have a question because I don't know what to do.
We are designing an interface that takes records from the table, edits the output, and returns them to the screen.
I am using java8, db2.

○About the processing summary
If there is a score table like the one below,

select semester, avg (Korean), avg (Math), avg (English) from score where name = 'Yamada' group by year

Stores the value returned by sql in the list type of such a map.

result=[{semester:xx, Korean:□□, Mathematics:△△, English:☆☆}, { }}, .]

Currently, it is counted by semester, but
I would like to display it in the following table format on the screen.
The value returned from the interface for display is also the list type of the map.

 [{Subject: Korean, 1st semester:○○, 2nd semester:xx, 3rd semester:△△}, { }},...]

Mr. Yamada's score:

I wonder if it's an image where rows and columns are swapped.
As a constraint, it is not possible to add tables to aggregate by subject and must be derived from the current score table format.
Therefore, we believe that the sql or java logic should enable matrix replacement.

In the case of sql, it seems that it can be achieved by uniting the following subjects, but I am worried about the performance because the number of union sessions is very high.
(Actually, there are about 100 subjects.)…)

select 'language' as subject
       max (CASE WHEN semester = '1'THEN language END) AS 1st semester,
       max(CASE WHEN semester = '2'THEN language END) AS 2nd semester,
       max(CASE WHEN semester='3'THEN language END) AS 3rd semester
from score 
where name = 'Yamada'
group by semester;

If it is possible to do so with java, I would like to have it output looped, but
I'm ashamed to say that I can't come up with a plan to deal with it…

Could you please let me know if there is a good way?

java sql algorithm db2

2022-12-07 02:04

1 Answers

If the key is 2D (subject, semester), I think it would be better to have a data structure that can be drawn in that group.

The following is an example implemented on Map:

Immediately after retrieved from DB:

varqueryResultList=List.of(
                Map. of ("Semester", 1, "Japanese", 62.33, "Math", 50.33, "English", 68.0),
                Map.of ("Semester", 2, "Japanese", 63.0, "Math", 58.0, "English", 66.33)
        );

{semester:{subject:average}}Convert to a map:


        varavages=queryResultList.stream().collect(Collectors.toMap(
                e->{
                    vartermValue=e.get("semester").intValue();
                    return Term.from(termValue);
                },
                e->{
                    varqrMap = new HashMap<>(e);
                    qrMap.remove("semester");

                    return qrMap.entrySet().stream().collect(Collectors.toMap(
                            f->Subject.from(f.getKey()) ,
                            f->f.getValue().doubleValue()
                    ));
                }
        ));

Sample Output:

for(Subjects:Subject.values()){
            for(Termt:Term.values()) {
                varavg=verages.get(t).get(s);
                System.out.printf("%.2f\t", avg);
            }
            System.out.println();
        }

Replace the subject loop and semester loop and place it:

for(Termt:Term.values()){
            for(Subjects:Subject.values()) {
                varavg=verages.get(t).get(s);
                System.out.printf("%.2f\t", avg);
            }
            System.out.println();
        }

Sample Implementation


2022-12-07 02:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.