Solved

Question about COUNT in sub-query

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

623 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