2013-12-09 2 views
0

의 조항이 내 쿼리 인 경우 :선택 탑 내부 조인 개수 및 SQL

ExamID StudentID ItemNo StudentAnswer Score 
1006 1   1  A    25 
1005 1   2  B    30 
1004 1   3  A    35 

내가 뭘 원하는 것은 그것이 StudentAnswer = 경우 2를 반환합니다 수 있습니다 :

SELECT TOP 3 tablestudentanswer.examid, 
      tablestudentanswer.studentid, 
      tablestudentanswer.itemno, 
      tablestudentanswer.studentanswer, 
      tablescore.score 
FROM tablestudentanswer 
     INNER JOIN tablescore 
       ON tablestudentanswer.studentid = tablescore.studentid 
        AND tablestudentanswer.examid = tablescore.examid 
WHERE tablestudentanswer.examid = 1 
     AND tablestudentanswer.itemno = 1 
ORDER BY tablescore.score ASC 

그것은이 테이블 반환 'A'와 1이면 StudentAnswer = 'B' 맨 위에 내 검색어에 아무런 문제가 없습니다. 내가 묻는 것은 그 쿼리에 무엇을 추가해야 하는가이다.

내 마음에 2를 반환해야하지만이 오류가 있습니다.

Select COUNT(*) From (
Select Top 3 TableStudentAnswer.ExamID, TableStudentAnswer.StudentID, TableStudentAnswer.ItemNo, TableStudentAnswer.StudentAnswer, TableScore.Score 
from TableStudentAnswer 
Inner join TableScore on TableStudentAnswer.StudentID=TableScore.StudentID and TableStudentAnswer.ExamID=TableScore.ExamID 
where TableStudentAnswer.ExamID=1 and TableStudentAnswer.ItemNo=1 
Order By TableScore.Score Asc) where TableStudentAnswer.StudentAnswer = 'A' 

그것은 반환해야합니다 :

2 

제발 도와주세요!

+2

귀하의 요구 사항을 명확히 설명해주십시오. 무엇이 잘못 되었습니까? –

+0

질문에 아무런 문제가 없으므로 원하는 출력을 얻기 위해 쿼리에 추가 할 내용을 모른다. StudentAnswer = 'A'이면 2이고 StudentAnswer = 'B'인 경우 1입니다. – user3083026

+0

더 혼란을 피하기 위해 실제로 원하는 결과 세트를 추가하십시오. 모든 행과 열과 그 값을 가져야합니다. 그것은 일반적으로 우리가 당신이 대답을 알고 싶어 대답하고 있는지 확인하는 가장 좋은 방법입니다 :) – Luaan

답변

2

이렇게 할 수 있습니까?

SELECT TOP 3 tablestudentanswer.examid, 
      tablestudentanswer.studentid, 
      tablestudentanswer.itemno, 
      tablestudentanswer.studentanswer, 
      tablescore.score, 
      case 
       when tablestudentanswer.studentanswer = 'A' then 2 
       when tablestudentanswer.studentanswer = 'B' then 1 
       else NULL 
      end as [MyColumn] 
FROM tablestudentanswer 
     INNER JOIN tablescore 
       ON tablestudentanswer.studentid = tablescore.studentid 
        AND tablestudentanswer.examid = tablescore.examid 
WHERE tablestudentanswer.examid = 1 
     AND tablestudentanswer.itemno = 1 
ORDER BY tablescore.score ASC 

귀하의 질문에 약간의 불확실성이 있습니다. 아마 당신은 각각에 대한 답변의 양을 원하십니까?

count(1) over (partition by tablestudentanswer.studentanswer) 

이렇게하면 결과 집합의 각 행에, 지정된 studentanswer 모든 답변의 양의 열을 제공 할 것입니다. 그러나 이것은 매우 느릴 수 있습니다. 가능한 경우 일반 group by을 사용하는 것이 좋습니다.

0

답변 수를 반환 하시겠습니까? 그렇다면 COUNT을 (를) 사용하면 도움이 될 수 있습니다.

SELECT tablestudentanswer.studentid, 
     tablestudentanswer.studentanswer 
     COUNT(1) AS NumberOfAnswers 
FROM tablestudentanswer 
    INNER JOIN tablescore 
      ON tablestudentanswer.studentid = tablescore.studentid 
       AND tablestudentanswer.examid = tablescore.examid 
GROUP BY tablestudentanswer.studentid, tablestudentanswer.studentanswer 

내가 잘못하면 저를 시정하십시오.

그런데 왜 SELECT 문에 결과 테이블이 있어도 결과 테이블이 itemno로 구성되지 않는 이유는 무엇입니까?

+0

죄송하지만 아이템을 넣어 두는 것을 잊지 마십시오. – user3083026

+0

다른 옵션을 사용하십시오 ... – user3083026

+0

다음 쿼리는 작업을 수행해야합니까? 선택 COUNT (1)에서 ( 선택 상위 3 StudentAnswer TableStudentAnswer 내부에서 TableStudentAnswer.StudentID = TableScore.StudentID 및 TableStudentAnswer.ExamID = TableScore.ExamID 에 TableScore에 참여할 경우 TableStudentAnswer.ExamID = 1 TableStudentAnswer.ItemNo = 1) where StudentAnswer = 'A' – ChunLin