Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with subquery
    primarykey
    data
    text
    <p>I need to run a report on the total amount of students per day. The issue I am having is that in my subquery I can not do a proper group by so that within the day I want to know how many came in for a specific reason. </p> <p>My query is : </p> <pre><code>SELECT DATE_FORMAT(session.signintime, '%b %d %Y') Date, COUNT(session.session_id) 'Total', (SELECT COUNT(aidyear) FROM session WHERE aidyear = '12-13') '12-13', (SELECT COUNT(aidyear) FROM session WHERE aidyear = '13-14') '13-14' FROM session WHERE status = '3' GROUP BY Date; </code></pre> <p>The resulting report is : </p> <pre><code>+-------------+-------+-------+-------+ | Date | Total | 12-13 | 13-14 | +-------------+-------+-------+-------+ | Apr 15 2013 | 47 | 38 | 25 | | Apr 16 2013 | 5 | 38 | 25 | +-------------+-------+-------+-------+ 2 rows in set (0.00 sec) </code></pre> <p>As you notice for April 16 2013 there is a total of 5 students only. Now notice that the 12-13 13-14 for both rows though are identical. </p> <p>When I try to do a group by within the sub query I get this error : </p> <blockquote> <blockquote> <p>ERROR 1241 (21000): Operand should contain 1 column(s)</p> </blockquote> </blockquote> <p>meaning the sub query is returning more then one row to my understanding.</p> <p>I tried with this query : </p> <pre><code>SELECT DATE_FORMAT(session.signintime, '%b %d %Y') Date, COUNT(session.session_id) 'Total', (SELECT DATE_FORMAT(session.signintime, '%b %d %Y') Date, COUNT(aidyear) FROM session WHERE aidyear = '12-13' GROUP BY Date) '12-13', (SELECT DATE_FORMAT(session.signintime, '%b %d %Y') Date, COUNT(aidyear) FROM session WHERE aidyear = '13-14' GROUP BY Date) '13-14' FROM session WHERE status = '3' GROUP BY Date; </code></pre> <blockquote> <blockquote> <p>Edit 1 As requested by Evan</p> </blockquote> </blockquote> <p>The table I am querying from is the session table only. I am not joining (as you noticed)</p> <p>The table has the following : </p> <pre><code>Primary Key : session_id Foreign Key : anum(which is a student ID) </code></pre> <p>Then we have: <code>why, aidyear, signintime, studentcomments, status</code> </p>
    singulars
    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.
 

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