Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a new column using CASE and maybe count
    text
    copied!<p>Right now, I have a query that gets me 3 columns.</p> <blockquote> <p><strong>SELECT</strong> a.studentID, a.classdetailID, c.course_title<br> <strong>FROM</strong> studentcoursedetails a, classdetails b, course c<br> <strong>WHERE</strong> b.classdetailID = a.classdetailID<br> <strong>AND</strong> b.courseID = c.courseID<br> <strong>ORDER BY</strong> c.course_title, b.classdetailID</p> </blockquote> <p>What I get is a column that shows the ID of the student that did the course, the ID of the class they were in and the title of the course itself. Something along these lines:</p> <pre><code>5---8----Airline Security Operations 4---8----Airline Security Operations 6---8----Airline Security Operations 4---9----Airline Security Operations 8---10---Airline Security Operations 5---10---Airline Security Operations 1---1----Airport Design and Construction 4---1----Airport Design and Construction 2---1----Airport Design and Construction 1---2----Airport Design and Construction </code></pre> <p>What I need is a table that shows me something like this:</p> <pre><code>course_title Number of Classes Number of students Airline Security Operations 3 4 </code></pre> <p>I thought of using <code>CASE</code>, but when I think about it, I just get lost. I'd appreciate your help.</p> <p>The answer, thanks to gbn:</p> <blockquote> <p><strong>SELECT</strong> c.course_title, COUNT( DISTINCT a.studentID ) AS "Students in Course" , COUNT( <strong>DISTINCT</strong> a.classdetailID ) AS "Total Classes"<br> <strong>FROM</strong> studentcoursedetails a<br> <strong>JOIN</strong> classdetails b ON b.classdetailID = a.classdetailID<br> <strong>JOIN</strong> course c ON b.courseID = c.courseID<br> <strong>GROUP</strong> BY c.course_title</p> </blockquote>
 

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