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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:46:09+00:00 2026-05-25T21:46:09+00:00

I have a table that tracks contact class state changes by date. The question

  • 0

I have a table that tracks contact class state changes by date. The question that I am trying to answer is what is the current state of all contacts on a certain date.

DROP TABLE IF EXISTS `contact_class_state`;
CREATE TABLE `contact_class_state` (
    `id` int unsigned NOT NULL AUTO_INCREMENT,
    `contact_id` int unsigned DEFAULT NULL, -- the contact
    `contact_class` int unsigned,
    `state_date` date,
PRIMARY KEY (`id`),
INDEX (`contact_id`)
) DEFAULT CHARSET=utf8;

INSERT INTO `contact_class_state` (`contact_id`, `contact_class`, `state_date`) VALUES
(1, 1, '2011-01-01'),
(2, 1, '2011-01-01'),
(3, 1, '2011-01-01'),
(4, 1, '2011-01-01'),
(5, 1, '2011-01-01'),
(1, 2, '2011-02-01'),
(3, 2, '2011-02-01'),
(5, 2, '2011-02-01'),
(1, 1, '2011-02-15'),
(5, 3, '2011-03-01');

For example, the following query:

SELECT contact_id, contact_class, state_date
FROM contact_class_state
WHERE state_date <= '2011-02-27'
ORDER BY contact_id, state_date DESC

returns

+------------+---------------+------------+
| contact_id | contact_class | state_date |
+------------+---------------+------------+
|          1 |             1 | 2011-02-15 |
|          1 |             2 | 2011-02-01 |
|          1 |             1 | 2011-01-01 |
|          2 |             1 | 2011-01-01 |
|          3 |             2 | 2011-02-01 |
|          3 |             1 | 2011-01-01 |
|          4 |             1 | 2011-01-01 |
|          5 |             2 | 2011-02-01 |
|          5 |             1 | 2011-01-01 |
+------------+---------------+------------+

While this is technically correct, I only need the first (or last if sorted ASC) row for each contact_id as the latest date will always give me current state of the contact, per the below:

+------------+---------------+------------+
| contact_id | contact_class | state_date |
+------------+---------------+------------+
|          1 |             1 | 2011-02-15 |
|          2 |             1 | 2011-01-01 |
|          3 |             2 | 2011-02-01 |
|          4 |             1 | 2011-01-01 |
|          5 |             2 | 2011-02-01 |
+------------+---------------+------------+

I am pretty sure a sub or a complex query would do the trick but I am having a mental block with the SQL. I am also open to other approaches to solve this issue.

Thanks!

  • 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-25T21:46:09+00:00Added an answer on May 25, 2026 at 9:46 pm

    If your query is in fact what you want (except then grouped by contact_id), then do exactly that.

    SELECT * FROM
    (SELECT contact_id, contact_class, state_date
    FROM contact_class_state
    WHERE state_date <= '2011-02-27'
    ORDER BY contact_id, state_date DESC) table1
    GROUP BY contact_id
    

    This is tested and works perfect.

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

Sidebar

Related Questions

I have a table that has redundant data and I'm trying to identify all
I have a log feature in my project that tracks all changes to a
I have a History Table in SQL Server that basically tracks an item through
I have table that contain date and time field. id|date|time ========= 1|01/01/2001|10:45 2|01/02/2002|11:45 3|01/03/2003|12:45
I have a table that tracks serial numbers and the number of licenses associated
I'm working in Sql Server 2005. I have an event log table that tracks
I have a table that tracks attendance in a course. The columns are the
So I have a table that tracks the updating of a projects status. Now
I have this big database table that tracks (currently in production), a persons medical
I have a table (tbl_MatterItem) in my DB that tracks the dates that items

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.