📜  如何在 SQL Server 中创建日报、周报和月报?

📅  最后修改于: 2022-05-13 01:54:47.976000             🧑  作者: Mango

如何在 SQL Server 中创建日报、周报和月报?

SQL Server 是一个多功能数据库,也是全世界最常用的数据库。在本文中,让我们看看 SQL 查询如何从 SQL Server 获取每日、每周和每月报告。

让我们开始创建数据库和示例详细信息

第 1 步:创建数据库

创建数据库的命令。这里 GEEKSFORGEEKS 是数据库名称。

询问:

CREATE DATABASE GEEKSFORGEEKS;

步骤 2:使数据库处于活动状态

询问:

USE GEEKSFORGEEKS;

输出:

第 3 步:创建表

询问:

CREATE TABLE [dbo].[ArticleDetails](
    [ArticleID] [int] IDENTITY(1,1) NOT NULL,
    [AuthorID] [int],
    [ArticleDate] [date] NULL,
    [NARRATION] [varchar](25) NULL
) ON [PRIMARY]
GO

第 4 步:将数据插入表中

询问:

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')

第 5 步:让我们先查询样本数据。

询问:

SELECT  * FROM ArticleDetails;

输出:

第6步:获取每天发布/创建的文章的数据。

询问:

SELECT GETDATE() AS CURRENTDATE
SELECT CONVERT(nvarchar(10),
GETDATE(),101) AS FORMATTEDCURRENTDATE

询问:

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

询问:

-- 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

在这里,我们正在检查与 currentDate 值匹配的“articleDate”列。 CurrentDate 可以通过 'GETDATE()'函数获得。使用 Convert,我们正在检查 'articleDate' 值。

第 7 步:获取每周发布/创建的文章的数据:

询问:

-- 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)

可以通过以下方式获得每周分组数据。

询问:

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

第 8 步:获取按月发布/创建的文章的数据。这里我们使用 BETWEEN 子句。开始日期应比当前日期早 30 天,因此以下列方式提供。

询问:

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)

每月分组数据:

询问:

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

通过以上方式,我们可以获得每日/每周/每月的数据。