Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Question about COUNT in sub-query

Posted on 2006-11-24
2
Medium Priority
?
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 500 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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.
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

715 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