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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:38:14+00:00 2026-05-14T22:38:14+00:00

I have a large data-set of emails sent and status-codes. ID Recipient Date Status

  • 0

I have a large data-set of emails sent and status-codes.

ID Recipient           Date       Status
 1 someone@example.com 01/01/2010      1
 2 someone@example.com 02/01/2010      1
 3 them@example.com    01/01/2010      1
 4 them@example.com    02/01/2010      2
 5 them@example.com    03/01/2010      1
 6 others@example.com  01/01/2010      1
 7 others@example.com  02/01/2010      2

In this example:

  • all emails sent to someone have a status of 1
  • the middle email (by date) sent to them has a status of 2, but the latest is 1
  • the last email sent to others has a status of 2

What I need to retrieve is a count of all emails sent to each person, and what the latest status code was.

The first part is fairly simple:

SELECT Recipient, Count(*) EmailCount
FROM Messages
GROUP BY Recipient
ORDER BY Recipient

Which gives me:

Recipient           EmailCount
someone@example.com 2
them@example.com    3
others@example.com  2

How can I get the most recent status code too?

The end result should be:

Recipient           EmailCount LastStatus
someone@example.com          2          1
them@example.com             3          1
others@example.com           2          2

Thanks.

(Server is Microsoft SQL Server 2008, query is being run through an OleDbConnection in .Net)

  • 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-14T22:38:15+00:00Added an answer on May 14, 2026 at 10:38 pm

    This is an example of a ‘max per group’ query. I think it is easiest to understand by splitting it up into two subqueries and then joining the results.

    The first subquery is what you already have.

    The second subquery uses the windowing function ROW_NUMBER to number the emails for each recipient starting with 1 for the most recent, then 2, 3, etc…

    The results from the first query are then joined with the result from the second query that has row number 1, i.e. the most recent. Doing it this way guarantees that you will only get one row for each recipient in the case that there are ties.

    Here is the query:

    SELECT T1.Recipient, T1.EmailCount, T2.Status FROM
    (
        SELECT Recipient, COUNT(*) AS EmailCount
        FROM Messages
        GROUP BY Recipient
    ) T1
    JOIN
    (
        SELECT
            Recipient,
            Status,
            ROW_NUMBER() OVER (PARTITION BY Recipient ORDER BY Date Desc) AS rn
        FROM Messages
    ) T2
    ON T1.Recipient = T2.Recipient AND T2.rn = 1
    

    This gives the following results:

    Recipient            EmailCount  Status  
    others@example.com   2           2       
    someone@example.com  2           1       
    them@example.com     3           1       
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data set in MongoDB that involves a large set of emails
I have a large data set with 11 columns and 100000 rows (for example)
I have a large data set that I'm working with in excel. About 1000+
Interpolating Large Datasets I have a large data set of about 0.5million records representing
I have a very large possible data set that I am trying to visualize
I have a large real 1-d data set called r. I would like plot:
I have a large set of data (a data cube of 250,000 X 1,000
I have a large set of data which I access via a generator/iterator. While
I have implemented paging for a large set of data in an application by
I have a costumer showing Notepad with a large set of data that looks

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.