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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:10:07+00:00 2026-05-26T18:10:07+00:00

I have a database table which stores membership renewals each year. When insert the

  • 0

I have a database table which stores membership renewals each year. When insert the renewal record, an ‘expiryDate’ column is written with a date (31/8/[nextyear]).

So, as an example, let’s say a member with memberID = 99 renews in 2007, 2008 and 2009, he will have 3 records (one for each year), with an ‘expiryDate’ recorded in each. If I do a

SELECT MAX(YEAR(expiryDate)) as maxExpiry 
  FROM renewals 
 WHERE memberID = 99

…I will get 2010 back.

What I would like to do is return ALL records where the MAX(YEAR(expiryDate)) is a given year.. for example,

SELECT * 
  FROM renewals 
 WHERE MAX(YEAR(expiryDate)) = '2010';

This query won’t work as an aggregation can’t be used in a where clause outside a subquery, but I can’t quite work out how to structure the subquery… or even if this could be done a better way than using a subquery.

  • 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-26T18:10:07+00:00Added an answer on May 26, 2026 at 6:10 pm

    A predicate based on an aggregated column uses a HAVING clause, rather than a WHERE.

    If you only need the memberID, this is straightforward enough:

    SELECT memberID
      FROM renewals
      GROUP BY memberID
        HAVING MAX(YEAR(expiryDate)) = 2010
    

    You can also do this as a sub-query if you need to get other columns from that table, ie:

    SELECT * FROM members
      WHERE memberID IN ( <<previous query>> )
    

    UPDATE

    It is correct as @OMG Ponies pointed out that this is not enough if you need to select additional columns from that one row in renewals. If that is required, you could use:

    SELECT * FROM renewals
      WHERE memberID IN ( SELECT memberID FROM renewals
                          GROUP BY memberID HAVING MAX(YEAR(expiryDate)) = 2010 )
        AND YEAR(expiryDate) == 2010
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an order table in MySQL database, having a field/column which stores the
I have a database table which stores products. Each product can have multiple colours.
I have a database table A which stores records, A has a primary key
I have a database table called Posts which stores all the information regarding an
I have a table in my database which stores a tree structure. Here are
I have a string column in a database table which maps to an Enum
I have an INT column in a SQL Server database which stores a value
I have an EMPLOYEE table in a SQL Server 2008 database which stores information
I have a database table transaction which stores all the transaction made by users
I have a database table which stores a type for a project which stores

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.