Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since the author was not kind enough to give the SQL to create the schema, here it is for anyone who wants to try the solution from @Eric.</p> <pre><code>create table markdetails (studid, subjectid, marks); create table student_info (studid, name); insert into markdetails values('A1', 3, 50); insert into markdetails values('A1', 4, 60); insert into markdetails values('A1', 5, 70); insert into markdetails values('B1', 3, 60); insert into markdetails values('B1', 4, 80); insert into markdetails values('C1', 5, 95); insert into student_info values('A1', 'Raam'); insert into student_info values('B1', 'Vivek'); insert into student_info values('C1', 'Alex'); </code></pre> <p>Here is an alternative solution using <code>case</code> with <code>group by</code>. </p> <pre><code>select si.studid, si.name, sum(case when md.subjectid = 3 then md.marks end) subjectid_3, sum(case when md.subjectid = 4 then md.marks end) subjectid_4, sum(case when md.subjectid = 5 then md.marks end) subjectid_5 from student_info si join markdetails md on md.studid = si.studid group by si.studid, si.name ; </code></pre> <p>For comparison, here is the same select statement from @Eric's solution:</p> <pre><code>select u.stuid, u.name, s3.marks as subjectid_3, s4.marks as subjectid_4, s5.marks as subjectid_5 from student_temp u left outer join markdetails s3 on u.stuid = s3.stuid and s3.subjectid = 3 left outer join markdetails s4 on u.stuid = s4.stuid and s4.subjectid = 4 left outer join markdetails s5 on u.stuid = s5.stuid and s5.subjectid = 5 ; </code></pre> <p>It will be interesting to see which one would perform better when there is a lot of data.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload