# Dashboard Report - Emails Sent

**URL:** https://meta.discourse.org/t/dashboard-report-emails-sent/289401
**Category:** Data & reporting
**Tags:** sql-query, dashboard-reports, dashboard-sql
**Created:** [December 21, 2023, 1:16am UTC](https://meta.discourse.org/t/dashboard-report-emails-sent/289401 "2023-12-21T01:16:03Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![SaraDev](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saradev/32/335139_2.png) [@SaraDev](https://meta.discourse.org/u/SaraDev)
#### Post date: [December 21, 2023, 1:16am UTC](https://meta.discourse.org/t/dashboard-report-emails-sent/289401/1 "2023-12-21T01:16:04Z")

</div>

This is an SQL version of the Dashboard Report for Emails Sent.

This report provides a daily breakdown of the total number of emails sent from the Discourse platform within a specified date range. It is useful for monitoring email activity, identifying trends, and ensuring that the email system is functioning correctly.

```sql
-- [params]
-- date :start_date
-- date :end_date

SELECT
  DATE(created_at) AS date_sent,
  COUNT(*) AS total_emails_sent
FROM email_logs
WHERE created_at BETWEEN :start_date AND :end_date
GROUP BY date_sent
ORDER BY date_sent ASC  

```

## SQL Query Explanation

The SQL query is designed to retrieve the count of emails sent each day within a given period. Here’s a step-by-step explanation of the query:

#### Parameters

The query accepts two parameters, `:start_date` and `:end_date`, allowing the user to specify the date range for the report. Both date parameters accept the date format of `YYYY-MM-DD` .

#### Query Breakdown

- `DATE(sent_at) AS date_sent`: Extracts the date part from the `sent_at` timestamp and labels the resulting column as `date_sent`.
- `COUNT(*) AS total_emails_sent`: Counts the total number of rows (emails) for each group, labeling the result as `total_emails_sent`.
- `FROM email_logs`: This clause indicates that the data will be retrieved from the `email_logs` table, which records each instance of an email being sent.
- `WHERE sent_at BETWEEN :start_date AND :end_date`: This clause filters the data to include only the records where the `sent_at` timestamp falls within the specified date range.
- `GROUP BY date_sent`: This clause groups the results by the date on which the emails were sent. This is necessary for the `COUNT` function to calculate the total per day.
- `ORDER BY date_sent ASC`: This clause orders the results in ascending order by the `date_sent` column, ensuring the report shows the data starting from the earliest date to the latest within the range.

### Example Results

| date\_sent | total\_emails\_sent |
| --- | --- |
| 2023-11-19 | 264 |
| 2023-11-20 | 932 |
| 2023-11-21 | 678 |
| 2023-11-22 | 637 |
| 2023-11-23 | 369 |
| … | … |

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [December 21, 2023, 1:24am UTC](https://meta.discourse.org/t/dashboard-report-emails-sent/289401/2 "2023-12-21T01:24:00Z")

</div>

It’s also worth noting that the default period for retaining email logs is 90 days, but can be adjusted using the `delete email logs after days` admin setting (with the option to set it to `0` to keep them indefinitely).

Just in case anyone wondered why looking at last years wasn’t working. 🙂
