create table LeaveInfo(Lid int primary key identity(1,1),StuNum nvarchar(50) not null,--学号,StuName nvarchar(50) not null,--姓名Phone nvarchar(30) not null,--手机号GNum nvarchar(20) not null,--级别CName nvarchar(20) not null,--班级BeginDate date not null,--开始时间EndDate date not null,--结束时间Addresss nvarchar(50) not null,--地点Reason nvarchar(200) not null,--原因Principal nvarchar(10) not null,--负责人Statu int ,--状态)上面是数据表,怎么编写sql语句实现像下图的功能,急急急急!
1 回答
慕标琳琳
TA贡献1830条经验 获得超9个赞
SELECT A.CName AS 班级名称, A.GNum AS 年级, (SELECT COUNT(*) FROM [dbo].[LeaveInfo] AS B WHERE B.CName = A.CName AND B.GNum = A.GNum AND B.[Statu] = 1 AND B.BeginDate >= DATEADD(DD,DATEDIFF(DD,0,GETDATE()),0) AND B.EndDate < DATEADD(DD,DATEDIFF(DD,0,GETDATE()),1)) AS 今日请假, (SELECT COUNT(*) FROM [dbo].[LeaveInfo] AS B WHERE B.CName = A.CName AND B.GNum = A.GNum AND B.[Statu] = 1 AND B.BeginDate >= DATEADD(DD,DATEDIFF(DD,0,GETDATE()),-1) AND B.EndDate < DATEADD(DD,DATEDIFF(DD,0,GETDATE()),0)) AS 昨日请假, (SELECT COUNT(*) FROM [dbo].[LeaveInfo] AS B WHERE B.CName = A.CName AND B.GNum = A.GNum AND B.[Statu] = 1 AND B.BeginDate >= DATEADD(WK,DATEDIFF(WK,0,GETDATE()),0) AND B.EndDate < DATEADD(WK,DATEDIFF(WK,0,GETDATE()),7)) AS 本周请假, (SELECT COUNT(*) FROM [dbo].[LeaveInfo] AS B WHERE B.CName = A.CName AND B.GNum = A.GNum AND B.[Statu] = 1 AND B.BeginDate >= DATEADD(MM,DATEDIFF(MM,0,GETDATE()),0) AND B.EndDate < DATEADD(MM,DATEDIFF(MM,0,GETDATE()) + 1,0)) AS 本月请假FROM [dbo].[LeaveInfo] AS A GROUP BY A.CName, A.GNum
- 1 回答
- 0 关注
- 1256 浏览
添加回答
举报
0/150
提交
取消