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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:05:47+00:00 2026-06-15T16:05:47+00:00

I have a T-SQL query that is designed to weed out duplicate entries of

  • 0

I have a T-SQL query that is designed to weed out duplicate entries of a certain product training, grabbing only the one with the most recent DateTaken. For example, if someone has taken a certain training course 3 times, we only want to display one row, that row being the one that contains the most recent DateTaken. Here is what I have so far, however I am receiving the following error:

An expression of non-boolean type specified in a context where a condition is expected, near ‘ORDER’.

The ORDER BY is necessary since we want to group all the results of this query by the expiration date. Below is the full query:

SELECT DISTINCT 
   p.ProductDescription as ProductDesc, 
   c.CourseDescription as CourseDesc, 
   c.Partner, a.DateTaken, a.DateExpired, p.Status 
FROM 
   sNumberToAgentId u, AgentProductTraining a, Course c, Product p 
WHERE 
    @agentId = u.AgentId 
    and u.sNumber = a.sNumber 
    and a.CourseCode = c.CourseCode 
    and (a.DateExpired >= @date or a.DateExpired IS NULL) 
    and a.ProductCode  = p.ProductCode 
    and (p.status != 'D' or p.status IS NULL)
GROUP BY
    (p.ProductDescription) 
HAVING 
    MIN(a.DateTaken)
ORDER BY 
    DateExpired ASC

EDIT

I’ve made the following changes to the GROUP BY and HAVING clauses, however I am still receiving errors:

GROUP BY
    (p.ProductDescription, c.CourseDescription) 
HAVING 
    MIN(a.DateTaken) > GETUTCDATE()

In SQL Management Studio, and red line error marker appears under the ‘,’ after p.ProductDescription, the ‘)’ after c.CourseDescription, the ‘a’ in a.DateTaken, and the closing parenthesis ‘)’ of GETUTCDATE(). If I simply leave the GROUP BY statement to include only p.ProductDescription I get this error message:

Column ‘Product.ProductDescription’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

I’m relatively new to SQL, could someone explain what’s going on? Thank you!

  • 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-15T16:05:47+00:00Added an answer on June 15, 2026 at 4:05 pm

    My suggestion since you are using sql server is to implement row_number() and partition by the ProductDescription and CourseDescription. This will go in a subquery and then you apply a filter to return only those where the row number is equal to one or the most recent record:

    select *
    from
    (
        SELECT   p.ProductDescription as ProductDesc, 
           c.CourseDescription as CourseDesc, 
           c.Partner, a.DateTaken, a.DateExpired, p.Status 
           row_number() over(partition by p.ProductDescription, c.CourseDescription order by a.DateTaken desc) rn
        FROM sNumberToAgentId u
        INNER JOIN AgentProductTraining a
            ON u.sNumber = a.sNumber 
            AND (a.DateExpired >= @date or a.DateExpired IS NULL) 
        INNER JOIN Course c
            ON a.CourseCode = c.CourseCode 
        INNER JOIN Product p 
            ON a.ProductCode  = p.ProductCode 
            AND (p.status != 'D' or p.status IS NULL)
        WHERE  u.AgentId = @agentId
    ) src
    where rn = 1
    order by DateExpired
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following sql query that take only 1 second to execute: select a.date,
I have this SQL Query that pulls data from 3 tables. I am unable
I have a SQL query that returns a Datatable: var routesTable = _dbhelper.Select(SELECT [RouteId],[UserId],[SourceName],[CreationTime]
I have a SQL query that I'm trying to debug. It works fine for
I have an SQL query that returns all companies records ignore the ones with
I have a SQL query that I'm trying to write, but I'm not quite
Suppose I have a SQL Query that isn't really nice. The SQL Query has
I currently have a SQL query that returns a number of fields. I need
I need to be able to have an SQL query that searches my database
i have a huge sql query that is attached to the rowsource of a

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.