Solved

Question about COUNT in sub-query

Posted on 2006-11-24
2
218 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
ID: 18009152
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
ID: 18009224
That did it, thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

830 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