Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6046019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:11:25+00:00 2026-05-23T07:11:25+00:00

Here is my problem (I’m using SQL Server) I have a table of Students

  • 0

Here is my problem (I’m using SQL Server)

I have a table of Students (StudentId, Firstname, Lastname, etc).

I have a table that records StudentAttendance (StudentId, ClassDate, etc.)

I record other student activity (I’m generalizing here for simplicity) such as a Papers table (StudentId, PaperId, etc.). There may be anywhere from zero to 20 papers turned in. Similarly, there is a table called Projects (StudentId, ProjectId, etc.). Same deal as with Papers.

What I’m trying to do is create a list of counts for students who have attendance over a certain level (say 10 attendances). Something like this:

ID    Name      Att    Paper Proj
123   Baker     23     0     2
234   Charlie   26     5     3
345   Delta     13     3     0

Here is what I have:

select 
  s.StudentId,
  s.Lastname,
  COUNT(sa.StudentId) as CountofAttendance,
  COUNT(p.StudentId) as CountofPapers
from Student s
inner join StudentAttendance sa on (s.StudentId = sa.StudentId)
left outer join Paper p on (s.StudentId = p.StudentId)
group by s.StudentId, s.Lastname
Having COUNT(sa.StudentId) > 10
order by CountofAttendance

If the CountofPaper and join (either inner or left outer) to the Papers table is commented out, the query works fine. I get a nice count of students who have attended at least 10 classes.

However, if I put in the CountofPapers and the join, things get crazy. With a left outer join, any students with papers just show their attendance count in the paper column. With an inner join, both attendance and paper counts seem to multiple off each other.

Guidance needed and appreciated.

Dave

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:11:26+00:00Added an answer on May 23, 2026 at 7:11 am

    The problem is there are multiple papers per student, so a StudentAttendance row for every row of Paper that joins: the counts will be re-added every time. Try this:

    select 
      s.StudentId,
      s.Lastname,
      (select COUNT(*) from StudentAttendance where s.StudentId = sa.StudentId) as CountofAttendance,
      (select COUNT(*) from Paper where s.StudentId = p.StudentId) as CountofPapers
    from Student s
    where (select COUNT(*) from StudentAttendance where s.StudentId = sa.StudentId) > 10
    order by CountofAttendance
    

    EDITED to incorporate issue with reference to CountofAttendance

    btw, this isn’t the fastest solution, but it is the easiest to understand, which was my intention. You can avoid the re-calculation by using a join to an aliased select, but as I said, this is the simplest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a problem here with an MSI deployment that I'm working on (using
I'm having a strange problem here... I have an ASP.NET 3.5 application that has
I have a problem here using .dbf files. I can make CRUD operations but
Here is problem I have: Table A id_a | name --------------------- 1 | name-A
I've got a strange problem here. I have a class that I'll call MainView
Hey all, have a slight problem here. In CakePHP I have a controller that
Just a small SVN problem here. I setup my own SVN server Setting up
I'm having a bit of a problem here. We have 2 urls let me
Here's a problem I ran into recently. I have attributes strings of the form
Here's the problem, you include multiple assemblies and add 'using namespaceX' at the top

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.