---
title: "SQL to calculate number of values for each day based on a max value from previous date"  
description: "SQL to calculate number of values for each day based on a max value from previous date"  
author: "Steilla Mitchel"  
published: 2023-07-30  
updated: 2023-07-31  
canonical: https://www.mindstick.com/forum/159367/sql-to-calculate-number-of-values-for-each-day-based-on-a-max-value-from-previous-date  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 1 minute  

---

# SQL to calculate number of values for each day based on a max value from previous date

[SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) to calculate the number of [values](https://www.mindstick.com/forum/327/sum-textbox-values) for each day based on a [max](https://www.mindstick.com/articles/1155/count-max-min-function-in-excel) [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) from the [previous](https://yourviews.mindstick.com/view/81421/unlock-2-0-is-just-like-previous-corona-lockdowns) [date](https://yourviews.mindstick.com/story/3869/propose-day-2024-exciting-date-ideas-for-you-and-your-partner)

## Replies

### Reply by Aryan Kumar

Sure, here is the SQL to calculate the number of values for each day based on a max value from the previous date:

SQL

```plaintext
SELECT
  date,
  COUNT(*) AS num_values
FROM
  (
    SELECT
      date,
      MAX(value) AS max_value
    FROM
      my_table
    GROUP BY
      date
  ) AS t
GROUP BY
  date
HAVING
  max_value IS NOT NULL
ORDER BY
  date;
```

This query will first calculate the maximum value for each day in the `my_table` table. It will then group the rows by date and count the number of rows for each date. The `HAVING` clause ensures that only days with a maximum value are included in the results. The `ORDER BY` clause sorts the results by date.

The following is an example of the output of the query:

```plaintext
date | num_values
------- | --------
2023-01-01 | 10
2023-01-02 | 20
2023-01-03 | 30
```


---

Original Source: https://www.mindstick.com/forum/159367/sql-to-calculate-number-of-values-for-each-day-based-on-a-max-value-from-previous-date

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
