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

  • SEARCH
  • Home
  • 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 5984371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:21:59+00:00 2026-05-22T22:21:59+00:00

DECLARE @CURRENTSCHOOL TABLE (STUDENT VARCHAR(8), COURSE VARCHAR(8), SCHOOL VARCHAR(2)) INSERT INTO @CURRENTSCHOOL VALUES (‘10000000′,’MCR1010′,’11’)

  • 0
DECLARE @CURRENTSCHOOL TABLE (STUDENT VARCHAR(8), COURSE VARCHAR(8), SCHOOL VARCHAR(2))
INSERT INTO @CURRENTSCHOOL VALUES ('10000000','MCR1010','11')
INSERT INTO @CURRENTSCHOOL VALUES ('12000000','MCR6080','11')
INSERT INTO @CURRENTSCHOOL VALUES ('13000000','MCR6090','15')

DECLARE @OTHERSCHOOLS TABLE (STUDENT VARCHAR(8), COURSE VARCHAR(8), SCHOOL VARCHAR(2))
INSERT INTO @OTHERSCHOOLS VALUES ('10000000','MCR1010','11')
INSERT INTO @OTHERSCHOOLS VALUES ('10000000','MCR1011','14')
INSERT INTO @OTHERSCHOOLS VALUES ('10000000','MCR1012','15')
INSERT INTO @OTHERSCHOOLS VALUES ('12000000','MCR6080','19')
INSERT INTO @OTHERSCHOOLS VALUES ('13000000','MCR6090','15')

For the above sample data. Two tables. Currentschool and Otherschools.

Currentschool is the current course that a student is on including the schoolcode,
and is the main table.

OtherSchools is potentially other courses that a student can go on, in differing schools.

I need to compare the currentschool table against the otherschools table matched using the student id number, and for every different schoolcode in otherschools, it needs to return a count.

eg:

Student:    OtherSchoolCount:
10000000    2        (because of 2 different school codes than than the current school)
12000000    1        (because of 1 different school code than than the current school)
13000000    blank    (because not a different school code)

Is this possible?

Many thanks

M.

  • 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-22T22:22:00+00:00Added an answer on May 22, 2026 at 10:22 pm
    SELECT cs.student, 
           COUNT(os.course) 
    FROM   @CURRENTSCHOOL cs 
           LEFT JOIN @OTHERSCHOOLS os 
             ON cs.student = os.student 
                AND cs.school <> os.school 
    GROUP  BY cs.student 
    

    outputs

    STUDENT  
    -------- -----------
    10000000 2
    12000000 1
    13000000 0
    

    If Null is really preferred over Zero then you can do this (or use the equivalent CTE)

    SELECT student, 
           CASE 
             WHEN coursecount = 0 THEN NULL 
             ELSE coursecount 
           END coursecount 
    FROM   (SELECT cs.student, 
                   COUNT(os.course) coursecount 
            FROM   @CURRENTSCHOOL cs 
                   LEFT JOIN @OTHERSCHOOLS os 
                     ON cs.student = os.student 
                        AND cs.school <> os.school 
            GROUP  BY cs.student) t 
    

    Which outputs

    student  courseCount
    -------- -----------
    10000000 2
    12000000 1
    13000000 NULL
    

    Update: NullIF could be put to use as alternative to the Case statement see What applications are there for NULLIF()?

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

Sidebar

Related Questions

Declare @BadDecimal varchar(5) Set @BadDecimal = '4.5' Declare @GoodDecimal Decimal Set @GoodDecimal = @BadDecimal
Given the following: declare @a table ( pkid int, value int ) declare @b
DECLARE @ID BIGINT set @ID = 1323 UPDATE School SET RegistrationFee = 'fee_' +
I declare a variable for a 64 bit counter as : long long call_count;
I declare a static char array, then I pass it to a function. How
Why does Visual Studio declare new classes as private in C#? I almost always
If you declare variables of type byte or short and attempt to perform arithmetic
In Oracle I can declare a reference cursor... TYPE t_spool IS REF CURSOR RETURN
Is there anyway to declare an object of a class before the class is
When you attempt to declare an unsigned variable in C#.NET with a value outside

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.