---
title: "SQL Query to Get Yearly Aggregated Data in SQL Server"  
description: "SQL Query to Get Yearly Aggregated Data in SQL Server"  
author: "Ravi Vishwakarma"  
published: 2024-07-16  
updated: 2024-07-16  
canonical: https://www.mindstick.com/forum/160930/sql-query-to-get-yearly-aggregated-data-in-sql-server  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2008", "sql server 2012", "sql server 2022"]  
reading_time: 2 minutes  

---

# SQL Query to Get Yearly Aggregated Data in SQL Server

I need to write a query to [aggregate](https://www.mindstick.com/blog/52/aggregate-functions-in-database) [sales](https://www.mindstick.com/articles/13125/tips-to-increase-sales-of-your-woocommerce-store) data by year from the `Sales` table. How can I achieve this in SQL Server?

## Replies

### Reply by Ravi Vishwakarma

To create a SQL query that aggregates data every year in SQL Server, you can use the `YEAR()` function along with `GROUP BY` to group your data by year.

```plaintext
Select YEAR(CreationDate) AS [Year], COUNT(*) AS TotalPosts
from Article
GROUP BY YEAR(CreationDate)
ORDER BY [Year]
```

## Explanation,

- `YEAR(CreationDate) AS Year`: Extracts the year from the `CreationDate` and labels it as `Year`.
- `COUNT(*) AS TotalPosts`: Count the column for each year.
- `GROUP BY YEAR(CreationDate)`: Groups the results by year.
- `ORDER BY Year`: Orders the results by year.

You can adjust the columns and table names as needed to fit your specific data structure. If you need more complex aggregations or additional columns, you can expand the `SELECT` clause and the `GROUP BY` clause accordingly.

## Read more

[**Help with Writing a Query to Combine Multiple Rows into One in SQL Server**](https://www.mindstick.com/forum/160929/help-with-writing-a-query-to-combine-multiple-rows-into-one-in-sql-server)

[**SQL Query to Get Distinct Values from a Column in SQL Server**](https://www.mindstick.com/forum/160927/sql-query-to-get-distinct-values-from-a-column-in-sql-server)

[**How to Write a Query to Get Data from the Last 7 Days in SQL Server?**](https://www.mindstick.com/forum/160925/how-to-write-a-query-to-get-data-from-the-last-7-days-in-sql-server)

[**How to drop a user in SQL Server?**](https://www.mindstick.com/forum/160924/how-to-drop-a-user-in-sql-server)


---

Original Source: https://www.mindstick.com/forum/160930/sql-query-to-get-yearly-aggregated-data-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
