Solved

Question about COUNT in sub-query

Posted on 2006-11-24
2
201 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Inner Join - Multiple Join Parameters 2 21
Update data using formula 22 21
recover sqlserver db 8 55
SSRS troubles 4 20
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

920 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

15 Experts available now in Live!

Get 1:1 Help Now