2

I have table to track the success, failure of students taking exam in a course as follows.

      Column   |  Type   |                        Modifiers                        
------------+---------+---------------------------------------------------------
 id         | integer | not null default nextval('assessment_id_seq'::regclass)
 student_id | integer | not null
 lesson_id  | integer | not null
 correct    | boolean | default false

Now, I need to generate a report of the students. Report just shows the number of attempts as total, and number of correct ones as score - per lesson.

select count(*) as score from assessment where correct = true and student_id = 1 group by lesson_id 

select count(*) as total_attempts from assessment  where student_id = 1 group by lesson_id .

I would like to combine these two queries to a single query. How can i do this..

Thanks.

1 Answer 1

3
SELECT COUNT(*) as total_attempts,
  COUNT(CASE WHEN correct = true THEN 1 ELSE NULL END) as score
FROM assessment
WHERE student_id = 1
GROUP BY lesson_id
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.