How to Create Daily, Weekly, and Monthly Report in SQL Server?
Last Updated :
16 Dec, 2021
SQL Server is a versatile database and a most used database throughout the world. In this article, let us see SQL queries how to get Daily, Weekly, and Monthly reports from SQL Server.
Let us start in creating a database and sample details
Step 1: Database creation
Command to create the database. Here GEEKSFORGEEKS is the db name.
Query:
CREATE DATABASE GEEKSFORGEEKS;
Step 2: Make the database active
Query:
USE GEEKSFORGEEKS;
Output:

Step 3: Table Creation
Query:
CREATE TABLE [dbo].[ArticleDetails](
[ArticleID] [int] IDENTITY(1,1) NOT NULL,
[AuthorID] [int],
[ArticleDate] [date] NULL,
[NARRATION] [varchar](25) NULL
) ON [PRIMARY]
GO
Step 4: Inserting data into table
Query:
INSERT INTO ArticleDetails (AuthorID,ArticleDate,
NARRATION) VALUES (1,'2021-12-10','MONGODB')
INSERT INTO ArticleDetails (AuthorID,ArticleDate,
NARRATION) VALUES (1,'2021-12-10','JAVA')
INSERT INTO ArticleDetails (AuthorID,ArticleDate,
NARRATION) VALUES (1,'2021-12-05','PYTHON')
INSERT INTO ArticleDetails (AuthorID,ArticleDate,
NARRATION) VALUES (1,'2021-11-10','WEB DEVELOPMENT')
INSERT INTO ArticleDetails (AuthorID,ArticleDate,
NARRATION) VALUES (1,'2021-11-08','DATABASE')
INSERT INTO ArticleDetails (AuthorID,ArticleDate,
NARRATION) VALUES (2,'2021-12-10','MONGODB')
INSERT INTO ArticleDetails (AuthorID,ArticleDate,
NARRATION) VALUES (2,'2021-12-01','PYTHON')
Step 5: Let us query the sample data first.
Query:
SELECT * FROM ArticleDetails;
Output:

Step 6: Get the data of the article published/created on a daily basis way.
Query:
SELECT GETDATE() AS CURRENTDATE
SELECT CONVERT(nvarchar(10),
GETDATE(),101) AS FORMATTEDCURRENTDATE

Query:
SELECT AuthorID,ArticleDate,NARRATION AS
'Daily' FROM ArticleDetails
WHERE ArticleDate = CONVERT(nvarchar(10),
GETDATE(),101);

Query:
-- To get the count of articles created/published
by the author on daily basis
SELECT AuthorID, COUNT(AuthorID) AS
'Daily Writeups' FROM ArticleDetails
WHERE ArticleDate = CONVERT(nvarchar(10),
GETDATE(),101) GROUP BY AuthorID

Here we are checking the ‘articleDate’ column matching with the currentDate value. CurrentDate can be obtained by means of ‘GETDATE()’ function. Using Convert, we are checking against the ‘articleDate’ value.
Step 7: Get the data of the article published/created on a WEEKLY basis way :
Query:
-- To get weekly
-- As we need to get the data between 7 days
from current date, we are using BETWEEN clause
-- start date should be 7 days earlier
from currentdate and hence it is provided in the below way
SELECT AuthorID,ArticleDate,NARRATION AS
'Weekly' FROM ArticleDetails
WHERE ArticleDate BETWEEN DATEADD(d,-7,
CONVERT(nvarchar(10),GETDATE(),101))
AND CONVERT(nvarchar(10),GETDATE(),101)

A Weekly groupwise data can be obtained in below way.
Query:
SELECT AuthorID, COUNT(AuthorID) AS
'Weekly Writeups' FROM ArticleDetails
WHERE ArticleDate BETWEEN DATEADD(d,-7,
CONVERT(nvarchar(10),GETDATE(),101))
AND CONVERT(nvarchar(10),GETDATE(),101)
GROUP BY AuthorID

Step 8: Get the data of the article published/created on a MONTHLY basis way. Here we are using BETWEEN clause. A start date should be 30 days earlier from currentdate and hence it is provided in the below way.
Query:
SELECT AuthorID,ArticleDate,NARRATION AS
'Monthly Writeups' FROM ArticleDetails
WHERE ArticleDate BETWEEN DATEADD(d,-30,
CONVERT(nvarchar(10),GETDATE(),101))
AND CONVERT(nvarchar(10),GETDATE(),101)

Monthly group wise data :
Query:
SELECT AuthorID, COUNT(AuthorID) AS
'Monthly Writeups' FROM ArticleDetails
WHERE ArticleDate BETWEEN DATEADD(d,-30,
CONVERT(nvarchar(10),GETDATE(),101))
AND CONVERT(nvarchar(10),GETDATE(),101)
GROUP BY AuthorID

In the above ways, we can able to get daily/weekly/monthly data.
Similar Reads
How to Group by Day, Date, Hour, Month or Year in SQL Server
Grouping data by day, date, hour, month, or year in SQL Server involves using the GROUP BY clause and appropriate date functions to extract or manipulate the relevant portions of the datetime column. The SQL Server GROUP BY operator is similar to the SQL GROUP BY Operator. The GROUP BY clause is a p
3 min read
How to Import and Export SQL Server Database?
Creating and managing a SQL Server database is an essential skill for database administrators and developers. In this article, We will go through the process of setting up a database in SQL Server, from creating the database and tables to inserting records, and finally, exporting and importing the d
3 min read
How to Exclude Weekend Days in a SQL Server Query?
With this article, we will learn how to exclude weekend days in a SQL server query. For this task, we use the DATEADD ( ) MS.SQL server function. This function in SQL Server is used, to sum up, a time or a date interval to a specified date then returns the modified date. Syntax : DATEADD(interval, n
2 min read
How to Find Day Name From Date in SQL Server?
Finding the day name from a specific date is a common task in SQL Server, useful for generating reports, analyzing trends, or scheduling. SQL Server provides two primary methods such as the DATENAME() function and the FORMAT() function. In this article, We will learn about How to Find Day Name From
4 min read
How to Select Data Between Two Dates and Times in SQL Server?
In SQL, some transactions need to be extracted based on their completion times and dates. Here, the DATETIME2 data type in SQL to perform such operations. For this article, we will be using the Microsoft SQL Server as our database. Note: Here, we will use the two dates and times given in the query a
2 min read
How to Join to First Row in SQL Server
Joining the first row in SQL Server can be a common requirement in various scenarios, such as when we need to retrieve specific data associated with the first occurrence of a particular group or when you want to fetch additional details related to the first record in a result set. In this article, W
4 min read
How to Extract Day of Week From Date Field in PostgreSQL?
In PostgreSQL, extracting the day of the week from a date field is a common and essential task when working with temporal data. Knowing the day of the week is crucial for various applications, including scheduling, reporting, and analytical purposes. PostgreSQL provides multiple methods to achieve t
5 min read
How to Open a Database in SQL Server?
Opening a database in SQL Server is a fundamental task for database administrators and developers. It involves establishing a connection to the server instance and selecting a database to work with. In this article, we will explore two methods to open a database in SQL Server such as using SQL Serve
3 min read
How to Compare Product Sales By Month in SQL?
A monthly sales report represents the state of sales activities in a company per month. It helps the sales team to align their efforts effectively. Whether we are a sales leader or manager, metrics play a crucial role in ensuring our company's success. If our data is stored in a database, SQL provid
4 min read
How to Alter Multiple Columns at Once in SQL Server?
In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database an
3 min read