Solved

Question about COUNT in sub-query

Posted on 2006-11-24
2
188 Views
Last Modified: 2008-01-09
Hello,

I have a number of Modules, each of which has a number of multiple choice questions.  

I need a query that will return A) The user's score, B)The Module name, C) The total number of questions in each Module.

The following query returns A & B successfully, but C returns the total number of questions of ALL Modules rather that in EACH Module.

SELECT sc.Score, sc.UserNameID, m.ModuleID, m.ModuleName, un.UserName,
      (SELECT COUNT(ModuleID) FROM Questions) AS QuestionCount
FROM ScoreByModule sc INNER JOIN Modules m
ON sc.ModuleID = m.ModuleID
INNER JOIN UserName un
ON sc.UserNameID = un.UserNameID
WHERE un.UserName ='JoeSmith'
GROUP BY m.ModuleName, sc.Score, sc.UserNameID, m.ModuleID, un.UserName

Thanks-

0
Comment
Question by:tmccrank
2 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 125 total points
Comment Utility
Give this a shot:

SELECT
    sc.Score,
    sc.UserNameID,
    m.ModuleID,
    m.ModuleName,  
    un.UserName,
    COUNT(q.ModuleID) AS QuestionCount
FROM
    ScoreByModule sc
    INNER JOIN Modules m
        INNER JOIN Questions q ON q.ModuleID=m.ModuleID
    ON sc.ModuleID = m.ModuleID
    INNER JOIN UserName un ON sc.UserNameID = un.UserNameID
WHERE
    un.UserName ='JoeSmith'
GROUP BY
    m.ModuleName, sc.Score, sc.UserNameID, m.ModuleID, un.UserName
0
 

Author Comment

by:tmccrank
Comment Utility
That did it, thanks.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now