How can I make multiple select statements on the same table?

扶醉桌前 提交于 2019-12-11 23:19:02

问题


I have a table that stores some student work info. I need to select the hours based on the studentID and the quarter ID. Here is what I have:

SELECT
(SELECT hours FROM clinicalStudents WHERE quarterID='201101' and studentID='$studentID') as q1,             
(SELECT hours FROM clinicalStudents WHERE quarterID='201102' and studentID='$studentID') as q2,             
(SELECT hours FROM clinicalStudents WHERE quarterID='201103' and studentID='$studentID') as q3,             
(SELECT hours FROM clinicalStudents WHERE quarterID='201104' and studentID='$studentID') as q4

It's only giving me some numbers but not all of them. I ran this (minus the WHERE clause) in my server manager and received an error:

"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression"

Any assistance would be great. Thanks!

EDIT:

The $studentID is generated in a while loop so I'm using the hours on that student before I move to the next one. I'm getting all the hours for one student in each quarter, adding them (this has to be done outside of the sql), storing results in a variable then moving to the next student. This works perfect when I get 1 quarter but i'm having an issue getting all the quarters.

EDIT Round 2: Did it in a rather lazy way I suppose:

I just selected all the hours and quarterID's for a specific student. Then ran a while(odbc_fetch_row()). If it was a 201101 I added it to the $q1 pile, 201102 added to the $q2 pile, and so on. Processing is a little slower but not a big issue with what I'm doing.


回答1:


trying use a SELECT TOP 1 or a LIMIT 1 in the query, depending which sql you are running.

EDIT Also, why are you trying to accomplish? This seems clunky and, depending your intended purpose, there is probably a better way available.




回答2:


I'm not sure what's your goal...

Maybe what you really want is:

select quarterID, sum(hours) 
from clinicalStudents 
where studentID='$studentID' 
group by 1



回答3:


SELECT 
hours, SUBSTR(quarterId, 4, 2)
FROM 
clinicalStudents 
WHERE 
quarterID IN ('201101', '201102', '201103', '201104') and studentID='$studentID'

Depending on what database you are using, you will have to change the function SUBSTR




回答4:


EDIT on loop.

Don't use a loop to do a SUM operation. Instead use the SUM aggregation operation

The allows you to project each quarter to each column.

   SELECT
 cs.studentid , q1.hours Q1, q2.hours Q2, q3.hours Q3, q4.hours Q4
    FROM 
    clinicalStudents cs
    (SELECT SUM(hours) hours , studentID 
     FROM clinicalStudents 
     WHERE quarterID='201101' and studentID='$studentID' 
     GROUP BY studentID ) q1
    LEFT join on cs.studentID = q1.clinicalStudents 
    (SELECT SUM(hours) hours , studentID  
     FROM clinicalStudents WHERE quarterID='201102' and studentID='$studentID' 
    GROUP BY studentID )  q2
    LEFT join on cs.studentID = q2.clinicalStudents              
    (SELECT SUM(hours) hours  , studentID 
     FROM clinicalStudents 
     WHERE quarterID='201103' and studentID='$studentID'
     GROUP BY studentID ) q3             
    LEFT join on cs.studentID = q3.clinicalStudents              
    (SELECT SUM(hours) hours , studentID 
     FROM clinicalStudents 
     WHERE quarterID='201104' and studentID='$studentID' GROUP BY studentID ) q4
    LEFT join on cs.studentID = q4.clinicalStudents          
    WHERE cs.studentID='$studentID'



回答5:


Try this.

select studentID
      ,sum(case when quarterID = '201101' then hours end) as q1
      ,sum(case when quarterID = '201102' then hours end) as q2
      ,sum(case when quarterID = '201103' then hours end) as q3
      ,sum(case when quarterID = '201104' then hours end) as q4
  from clinicalStudents
 where quarterID in('201101', '201102', '201103', '201104')
 group by studentID;


来源:https://stackoverflow.com/questions/4391903/how-can-i-make-multiple-select-statements-on-the-same-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!