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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:15:22+00:00 2026-05-24T08:15:22+00:00

I have a table that tracks emails sent from applications on my server. I

  • 0

I have a table that tracks emails sent from applications on my server. I would like to write a query that shows how many emails were sent by each application in a certain time period. Here is the table:

---------------------------------------------------------- 
|  emailID  |          SentDT          | ApplicationName | 
---------------------------------------------------------- 
|    1      |  2011-08-04 14:43:31.080 |    Term Form    | 
---------------------------------------------------------- 
|    2      |  2011-08-04 13:59:46.062 |    Term Form    |
---------------------------------------------------------- 
|    3      |  2011-08-03 10:38:15.015 |  Request Form   |
---------------------------------------------------------- 
|    4      |  2011-08-03 05:52:29.005 |    Term Form    |
---------------------------------------------------------- 
|    5      |  2011-08-01 19:58:31.094 | Recruiting Form |  
----------------------------------------------------------

I would like to see number of emails sent Today, Last 24 hours, Last 7 days, This Month, Last Month, All time.

I know how to do each of these queries by themselves, but I have no clue how to do it in one trip to the database.

For example:

-------------------------------------------------------------- 
|  ApplicationName |  Today | Last24 | Last7days | ThisMonth |
-------------------------------------------------------------- 
|    Term Form     |    2   |   5    |   10      |    19     |
--------------------------------------------------------------
|   Request Form   |    9   |   18   |   36      |    75     |
--------------------------------------------------------------
|  Recruiting Form |    15  |   35   |   100     |    250    |
--------------------------------------------------------------

I tried using a nested select for each subset of times, but I can’t use a group by in the nested select. My query that doesn’t produce results:

select COUNT(emailID), ApplicationName, (select COUNT(emailID) from emaillog where SentDT > '08/02/2011') as TwoDaysAgo
 from emaillog
 group by ApplicationName
 order by ApplicationName
  • 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-24T08:15:23+00:00Added an answer on May 24, 2026 at 8:15 am

    I think it’s much easier to do all the date calculations up front, then you can refer to local variables with logical names instead of embedding all the datediff/case etc. calculations in the query logic.

    Made a couple of assumptions here. (1) that no data in EmailLog is in the future (2) that by “Last 7 days” you mean today and the full 6 days preceding. I’ve also included a grand total – even though it’s not listed in your desired output, it seems you were trying to get it with the COUNT() outside the subquery.

    DECLARE @now SMALLDATETIME = SYSDATETIME();
    
    DECLARE @today DATE = @now, 
            @24hrsago SMALLDATETIME = DATEADD(DAY, -1, @now);
    
    DECLARE @7daysago DATE = DATEADD(DAY, -6, @today),
            @ThisMonth DATE = DATEADD(DAY, 1-DATEPART(DAY, @today), @today);
    
    --SELECT @now, @today, @24hrsago, @7daysago, @ThisMonth;
    
    WITH d AS
    (
        SELECT ApplicationName, c = COUNT(*)
        FROM EmailLog
        GROUP BY ApplicationName
    ),
    g AS
    (
        SELECT
            ApplicationName,
            [Today]     = SUM(CASE WHEN SentDt >= @today     THEN 1 ELSE 0 END),
            [Last24]    = SUM(CASE WHEN SentDt >= @24hrsago  THEN 1 ELSE 0 END),
            [Last7Days] = SUM(CASE WHEN SentDt >= @7daysago  THEN 1 ELSE 0 END),
            [ThisMonth] = SUM(CASE WHEN SentDt >= @ThisMonth THEN 1 ELSE 0 END)
        FROM EmailLog
        GROUP BY ApplicationName
    )
    SELECT d.ApplicationName,
        Total = d.c,
        [Today] = COALESCE(g.[Today], 0),
        [Last24] = COALESCE(g.[Last24], 0),
        [Last7days] = COALESCE(g.Last7days, 0),
        [ThisMonth] = COALESCE(g.ThisMonth, 0)
    FROM d LEFT OUTER JOIN g
    ON d.ApplicationName = g.ApplicationName;
    

    EDIT

    If my assumption was wrong and you don’t need the total count by application name, the query becomes much simpler:

    DECLARE @now SMALLDATETIME = SYSDATETIME();
    
    DECLARE @today DATE = @now, 
            @24hrsago SMALLDATETIME = DATEADD(DAY, -1, @now);
    
    DECLARE @7daysago DATE = DATEADD(DAY, -6, @today),
            @ThisMonth DATE = DATEADD(DAY, 1-DATEPART(DAY, @today), @today);
    
    SELECT ApplicationName,
        [Today]     = SUM(CASE WHEN SentDt >= @today     THEN 1 ELSE 0 END),
        [Last24]    = SUM(CASE WHEN SentDt >= @24hrsago  THEN 1 ELSE 0 END),
        [Last7Days] = SUM(CASE WHEN SentDt >= @7daysago  THEN 1 ELSE 0 END),
        [ThisMonth] = SUM(CASE WHEN SentDt >= @ThisMonth THEN 1 ELSE 0 END)
    FROM EmailLog
    GROUP BY ApplicationName;
    

    Ordering optional of course.

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

Sidebar

Related Questions

I have a table where I save emails that have been sent. I decided
I'd like to send emails from a table every night at midnight, so I've
Currently we have a table that we use to track inivitations. We have an
I have a database that tracks players through their attempts at a game. To
I have a log feature in my project that tracks all changes to a
I have a model for a Playlist object that has a number of Tracks.
I have two different tables to track location of equipment. The equipment table tracks
I don't want to have the security question and answer feature that ASP.Net Membership
I have a typical table, e.g. id(int) name(varchar) address(varchar) date(datetime) I also have a
Object-Relational-Mappers have been created to help applications (which think in terms of objects) deal

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.