我是一名学校教师,对MS SQL服务器非常新。每个人都建议我试试这个网站。就开始吧!
我正在尝试编写查询来测试不同类型的参与学术计划的结果措施。有几种不同的方法可以计算这个结果测量值,我想尝试一下。我正在尝试计算的结果是:在项目六个月期间保留参与者的百分比是多少? 我正在测试不同的参与者定义和不同的时间范围。我要生成4个查询。不幸的是,我必须使用4个不同的表格:Attendance, Status, Deenrolled, Inactive。我已经在下面包含了每个表格的样本数据。
查询
- 参与者被定义为在2012年7月1日至2013年6月30日这个财政年度内每周至少上两次课程的所有人,共181天。如果参与者被退学或失活,则会被删除。
- 参与者被定义为从2013年1月1日开始,在6个月(共181天)内每周至少上两次课程。如果参与者退学或成为失活,则会被删除。
- 参与者被定义为从2013年1月1日开始至今每周至少上两次课程的所有人。
- 参与者被定义为学生注册开始日期到他们退学或失活之间的时间。
参与者(分子):参与者/服务过的所有学生(分母)
我正在寻找的4个查询输出是这个的不同版本:
例子:
Participants Served Percent_Served
75 100 75%
我一直在尝试使用下面不同版本的查询
SELECT
Count (distinct ID) as Count,
Count ( DATEADD( dd, -181, DATEADD(wk, DATEDIFF(wk,0,Date), 0)) > 2 as Participants ,
FROM Attendance
where Attendence_date date between '07/01/2012' and '06/30/2013'
and ID not in (Select ID from Inactive)
or ID not in (select ID from Deenrolled)
GROUP BY ID
并且。
SELECT
Count (distinct ID) as Count,
Count ( DATEADD( dd, -181, DATEADD(wk, DATEDIFF(wk,0,Date), 0)) - Enrolled_Date as Participants ,
FROM Attendance
where Attendence_date date between '07/01/2012' and '06/30/2013'
and ID not in (Select ID from Inactive)
or ID not in (select ID from Deenrolled)
GROUP BY ID
非常感谢您为这些查询提供的任何编程帮助。
以下是示例数据集。
Attendence_date 是学生参加一节课的日期。
CREATE TABLE Attendance (
ID int,
Attendence_date datetime
)
INSERT INTO Attendance VALUES
(4504498, '7/1/2012'),
(4504498, '7/2/2012'),
(4504498, '7/3/2012'),
(4504498, '7/4/2012'),
(4504498, '7/5/2012'),
(4504498, '7/8/2012'),
(4504498, '7/9/2012'),
(4504498, '7/10/2012'),
(4504498, '7/11/2012'),
(4504498, '7/12/2012'),
(4504498, '7/1/2012'),
(4504498, '7/2/2012'),
(4504498, '7/3/2012'),
(4504498, '7/4/2012'),
(4504498, '7/5/2012'),
(4504498, '7/8/2012'),
(4504498, '7/9/2012'),
(4504498, '7/10/2012'),
(4504498, '7/11/2012'),
(4504498, '7/12/2012'),
(9201052, '7/15/2012'),
(9201052, '7/16/2012'),
(9201052, '7/17/2012'),
(9201052, '7/17/2012'),
(9201052, '7/18/2012'),
(7949745, '7/17/2012'),
(7949745, '7/18/2012'),
(7949745, '7/23/2012'),
(7949745, '7/23/2012'),
(7949745, '7/24/2012'),
(7949745, '7/26/2012'),
(7949745, '7/26/2012'),
(7949745, '8/8/2012'),
(7949745, '8/8/2012'),
(7949745, '11/5/2012'),
(7949745, '11/5/2012'),
(7949745, '11/5/2012'),
(7949745, '11/6/2012'),
(7949745, '11/6/2012'),
(7949745, '11/6/2012'),
(7949745, '11/7/2012'),
(7949745, '11/7/2012'),
(7949745, '11/7/2012')
这里包含了注册日期。
CREATE TABLE [Status] (
ID int,
Intake_Date datetime ,
Engaged_Date datetime ,
Enrolled_Date datetime)
INSERT INTO [Status] VALUES
(7949745, '3/7/2012', '7/17/2012', '3/8/2012'),
(4504498, '2/21/2013', '3/5/2013', '3/22/2013'),
(1486279, '4/18/2013', '5/7/2013', '5/20/2013'),
(9201052, '5/15/2012', '7/13/2012', '5/15/2012'),
(1722390, '3/5/2012', '8/27/2012', '3/8/2012'),
(7735695, '9/7/2012', '9/7/2012', '9/28/2012'),
(9261549, '3/7/2012', '7/24/2012', '3/8/2012'),
(3857008, '3/15/2013', '3/18/2013', '4/3/2013'),
(8502583, '3/14/2013', '4/15/2013', '5/3/2013'),
(1209774, '4/19/2012', '1/1/2012', '4/24/2012')
这里包含了取消注册的日期。
CREATE TABLE Deenrolled (
ID int,
Deenrolled_Date datetime)
INSERT INTO Deenrolled VALUES
(7949745, '2/4/2013'),
(5485272, '07/08/2013'),
(8955628, '01/10/2013'),
(5123221, '7/8/2013'),
(5774753, '7/18/2013'),
(3005451, '2/18/2013'),
(7518818, '05/29/2013'),
(9656985, '6/20/2013'),
(2438101, '7/17/2013'),
(1437052, '7/25/2013'),
(9133874, '4/25/2013'),
(7007375, '6/19/2013'),
(3178181, '5/24/2013')
未激活
CREATE TABLE Inactive (
ID int,
Effect_Date datetime)
INSERT INTO Inactive VALUES
(1209774, '10/12/2012'),
(5419494, '10/12/2012'),
(4853049, '10/9/2012'),
(1453678, '5/23/2013'),
(1111554, '7/16/2012'),
(5564128, '2/15/2013'),
(1769234, '7/16/2012')