0

I have been trying to get a percentage of concerns from each test however I am having trouble combining the two queries. Where I am having trouble is the divide by and getting the correct query. I used How to calculate percentage with a SQL statement for reference.

Table A

SELECT exams_id as exams, Count(*) AS TotalExamQuestions
FROM exams_questions AS eq 
JOIN concerns as con ON eq.concerns_id = con.concerns_id
GROUP BY exams_id
ORDER BY exams_id

Table B

SELECT e.exams_description, eq.exams_id as exams, con.concerns_id as ConcernID, count(*) as NumberOfConcerns
FROM exams_questions as eq
JOIN concerns as con ON eq.concerns_id = con.concerns_id
JOIN exams as e ON e.exams_id = eq.exams_id
WHERE eq.exams_id is not null 
GROUP BY eq.exams_id, con.concerns_id, con.concerns_description, e.exams_description
ORDER BY eq.exams_id asc, con.concerns_id 

Combine Table:

SELECT e.exams_description, eq.exams_id as exams, con.concerns_id as ConcernID,  Count(*) as numberofQuestions, Cast(Count(*)* 100.0 / Sum(Count(*)) OVER() AS DECIMAL(18, 2)) as ExamPercent
FROM exams_questions as eq
JOIN concerns AS con on eq.concerns_id = con.concerns_id
JOIN exams AS e on e.exams_id = eq.exams_id
GROUP BY eq.exams_id, con.concerns_id, e.exams_description
ORDER BY eq.exams_id asc, con.concerns_id

This works great if there is 100 exam questions. However the exams vary in length. I believe the line to change is:

 Cast(count(*)* 100.0 / sum(count(*)) over() AS DECIMAL(18, 2)) as ExamPercent 

How would I go about making this dynamic to the number of questions from table A?

Thanks for any guidance.

Community
  • 1
  • 1
Jay
  • 404
  • 6
  • 18
  • Here is a great place to start.http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ – Sean Lange Sep 20 '16 at 20:17
  • Are you trying to get the number of questions present out of a overall total question count or a percent correct of total questions? If percent correct then where is it that you are determining if a question is correct or incorrect? – Ross Bush Sep 20 '16 at 20:22
  • Number of questions out of the total in the exam. This is an exam breakdown of what % of each category is being asked. – Jay Sep 20 '16 at 21:18
  • It's hard to offer any advice without knowledge of at least some sample data, your current output, expected result, etc. – BJones Sep 20 '16 at 22:07

0 Answers0