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 7745889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:08:13+00:00 2026-06-01T10:08:13+00:00

It’s possible that this question has already been asked, but I can’t seem to

  • 0

It’s possible that this question has already been asked, but I can’t seem to find my answer, so here goes:

I’m selecting 10 fields from a JOIN on two tables. The result is about 1186 rows of data. In these rows, several are duplicates for all intents and purposes, except for one unique field (ClassId). I need this field, but it’s keeping me from getting and what I have defined as “Unique”. For example, querying on only Code, TeacherDescrip, and Term yields 1120 records.

I was hoping to run this same query, but add a WHERE clause with a subquery that searched for a narrower set of fields, the three listed above to be exact, that excluded that unique field that was causing my “duplicates”. Of course, the error I received is below:

“Only one expression can be specified in the select list when the
subquery is not introduced with EXISTS.”

When i tried to use EXISTS in stead of ClassScedule.Code IN I still got back the full 1186.

here is my query:

 SELECT DISTINCT ClassId
      ,Code
      ,Section
      ,Course
      ,Students
      ,ClassStart
      ,TeacherDescrip
      ,AdTeacherID
      ,email
      ,Term
      ,Campus
  FROM ClassScedule
  JOIN staff ON staff.StaffID = ClassScedule.AdTeacherID
  WHERE ClassStart BETWEEN '2012-03-01' AND '2012-03-30'
        AND ClassScedule.Code IN 
              (SELECT DISTINCT ClassScedule.Code, TeacherDescrip, Termcode 
               FROM ClassScedule 
              WHERE ClassStart BETWEEN '2012-03-01' AND '2012-03-30')
        AND TeacherDescrip IS NOT NULL 
  ORDER BY Instructor
  • 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-06-01T10:08:15+00:00Added an answer on June 1, 2026 at 10:08 am

    If you are using SQL Server 2005 or later, you can select one result for each distinct (Code, TeacherDescrip, Term) as follows:

    SELECT TOP (1) WITH TIES
           ClassId
          ,Code
          ,Section
          ,Course
          ,Students
          ,ClassStart
          ,TeacherDescrip
          ,AdTeacherID
          ,email
          ,Term
          ,Campus
      FROM ClassScedule
      JOIN staff ON staff.StaffID = ClassScedule.AdTeacherID
      WHERE ClassStart BETWEEN '2012-03-01' AND '2012-03-30'
      AND TeacherDescrip IS NOT NULL 
      ORDER BY ROW_NUMBER() OVER (
        PARTITION BY Code, TeacherDescrip, Term
        ORDER BY ClassID DESC
      )
    

    Among duplicate (Code, TeacherDescrip, Term) values, this query gives you the result with the smallest ClassID. If you want the largest instead, remove DESC.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.