练习1.按月份分析销售数据。
create database date_db;
use date_db;
CREATE TABLE SalesData (
SaleID INT PRIMARY KEY IDENTITY(1,1),
ProductName NVARCHAR(100) NOT NULL,
SaleAmount DECIMAL(10, 2) NOT NULL,
SaleDate DATE NOT NULL
);
INSERT INTO SalesData(ProductName, SaleAmount, SaleDate)
VALUES ('Product A', 150.00, '2024-01-15'),
('Product B', 200.00, '2024-02-05'),
('Product C', 120.50, '2024-03-01'),
('Product A', 250.00, '2024-03-20'),
('Product D', 180.75, '2024-04-10'),
('Product B', 300.00, '2024-04-15'),
('Product C', 220.00, '2024-05-01');
期望得到的结果:
月份 总销售额
1 150
2 200
3 370.5
4 480.75
5 220查询语句:
select datepart(Month,SaleDate) as 月份, sum(SaleAmount) from SalesData group by datepart(Month,SaleDate);
练习2.按星期分析销售数据。
期望得到的结果:
星期 总销售额
星期二 650
星期四 650.75
星期六 120.5查询语句:
select case datepart(weekday,SaleDate) when '1'then'星期一' when'2'then '星期二' when'3'then '星期三' when'4'then '星期四' when'5'then '星期五' when'6'then '星期六' when'7'then '星期七' endas 星期, sum(SaleAmount) from SalesData group by datepart(weekday,SaleDate);
运行结果:
练习3:按月份分析销售数据,并携带季度信息。
CREATE TABLE SalesRecords (
SaleID INT PRIMARY KEY IDENTITY(1,1),
ProductName NVARCHAR(50) NOT NULL,
SaleAmount DECIMAL(10,2) NOT NULL,
SaleDate DATE NOT NULL
);INSERT INTO SalesRecords(ProductName, SaleAmount, SaleDate)
VALUES ('Laptop', 1500.00, '2022-01-15'),
('Smartphone', 800.00, '2022-02-20'),
('Headphones', 50.00, '2022-03-05'),
('Tablet', 200.00, '2022-04-10'),
('Printer', 120.00, '2022-05-15'),
('Monitor', 300.00, '2022-06-20'),
('Keyboard', 30.00, '2022-07-05'),
('Mouse', 20.00, '2022-08-10');期望得到的结果如下:
月份 季度 总销售额
1 第一节度 1500
2 第一节度 800
......
7 第三季度 30查询语句:
select 月份,case when 月份 between 1 and 3 then '第一季度' when 月份 between 4 and 6 then '第二季度' when 月份 between 7 and 9 then '第三季度' when 月份 between 10 and 12 then '第四季度' end as 季度,总销售额 from (select month(saledate) 月份,cast(sum(saleamount) as int) as总销售额 from SalesRecords group by month(saledate))tb1111;
运行结果:
练习4:格式化订单日期(按要求格式转换)和总价(保留两位小数)
CREATE TABLE Orders (
OrderID INT PRIMARY KEY IDENTITY(1,1),
CustomerName NVARCHAR(100) NOT NULL,
OrderDate DATETIME NOT NULL,
TotalAmount FLOAT NOT NULL
);
INSERT INTO Orders(CustomerName, OrderDate, TotalAmount)
VALUES ('Alice Johnson', '2024-04-01 15:30:00', 250.992),
('Bob Smith', '2024-04-05 10:15:00', 185.521),
('Carolyn White', '2024-04-10 08:45:00', 300.752);期望得到的结果:
OrderID CustomerName OrderDate TotalAmount
1 Alice Johnson 2024/04/01 250.99
2 Bob Smith 2024/04/05 185.52
......
*/查询语句:
select OrderID, CustomerName, format(OrderDate,'yyyy/MM/dd')as OrderDate , round(TotalAmount,2) as TotalAmount from Orders;
运行结果:
练习5:员工生日提醒,查询出本月过生日的员工,并显示出此员工距离过生日还有几天。
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY IDENTITY(1,1),
Name NVARCHAR(100) NOT NULL,
DOB DATE NOT NULL
);
INSERT INTO Employees (Name, DOB)
VALUES ('Alice Johnson', '1990-05-15'),
('Bob Smith', '1985-06-20'),
('Carolyn White', '1992-05-01'),('David rose', '1988-07-10'),
('David Brown', '1988-04-10');
期望得到的结果:
EmployeeID Name DOB 生日提醒
2 David rose 1988-07-10 David rose距离过生日还有3天提示:concat()函数,可用于拼接数据。比如:
select concat('a','b'); 拼接后返回:ab
select concat(name,age) from tb1; 将tb1表的name列和age列数据拼接到一起。查询语句:
select *, concat(name,'距离过生日还有',datediff(day,day(DOB),day(getdate())),'天') as 生日提醒 from Employees where month(DOB)=month(getdate());
运行结果: