sql - How to merge a Group of records in oracle? -
consider following table,
name | subject_1 | marks_1 | subject_2 | marks_2 | tom | maths | 90 | | | tom | | | science | 50 | jon | | | science | 70 | jon | maths | 60 | | |
how following result
name | subject_1 | marks_1 | subject_2 | marks_2 | tom | maths | 90 | science | 50 | jon | maths | 60 | science | 70 |
tried forms of group by
did not correct result, maths come under subject_1 , science under subject_2.
use:
- max
- group by
sql> select name, 2 max(subject_1) subject_1, 3 max(marks_1) marks_1, 4 max(subject_2) subject_2, 5 max(marks_2) marks_2 6 t 7 group name; name subject_1 marks_1 subject_2 marks_2 ---- --------- ---------- --------- ---------- jon maths 60 science 70 tom maths 90 science 50 sql>
on side note, need think table design. have 3 columns, name, subject, marks
.
if want have separate columns in same table, should have them single row each student. , when have new subject student, update row student, instead of adding new row.
Comments
Post a Comment