早上好,
我正在尝试从数据集中确定连续日期(不包括星期日)的实例。数据存储在Access中,我将所需的日期提取到Excel中。然后我试图确定每个人在提供的数据中有多少个实例。示例如下。
数据示例:
| Name | Date of absence|
| Bob | 02/01/17 |
| Jill | 02/01/17 |
| Bob | 03/01/17 |
| Jill | 04/01/17 |
结果示例:
Bob - 1 Instance, 2 days
Jill - 2 Instance, 2 days
我开始尝试使用VBA在Excel中使用循环来轮换每个缺席实例直到所有人都已完成/勾选,但是代码变得非常麻烦并且感觉非常低效,更不用说如何这是为了获得更大的数据集!我想知道是否有可能在数据库中查询信息或写一些更高效的东西。
任何帮助或建议都将不胜感激!
更新
测试汤姆的建议;
Sql = "SELECT Absence.Racf,count(RecordDate) as dups"
Sql = Sql & " FROM Absence"
Sql = Sql & " left outer join"
Sql = Sql & " (select Racf, [RecordDate]+IIf(Weekday([RecordDate],7)=1,2,1) as date1 from Absence) t1"
Sql = Sql & " on Absence.RecordDate=t1.date1 and Absence.Racf=t1.Racf"
Sql = Sql & " where date1 Is Not Null"
Sql = Sql & " group by Absence.Racf"
但不幸的是,在下面的日期列表中,它返回7而不是5.
日期: 23月 - 16,24 - 2月 - 16,08 - 8 - 16,09 - 8 - 16,10 - 8 - 16,31 - 8 - 16,24-OCT-16,25-OCT-16,26-辛16,25-JAN-17,26-JAN-17,27-JAN-17
答案 0 :(得分:1)
这就是SQL在Access查询中实际看起来的方式
SELECT table1.name,count(date) as dups
FROM Table1
left outer join
(select name, [date]+IIf(Weekday([Date],7)=1,2,1) as date1 from table1) t1
on table1.date=t1.date1 and table1.name=t1.name
where date1 is not null
group by table1.name
;
如果您想使用宏从Excel运行此功能,here是一个有用的参考。
我从那里解除了代码并将设置SQL查询字符串的行更改为
SQL = "SELECT table1.name,count(date) as dups"
SQL = SQL & " FROM table1"
SQL = SQL & " left outer join"
SQL = SQL & " (select name, [date]+IIf(Weekday([Date],7)=1,2,1) as date1 from table1) t1"
SQL = SQL & " on table1.date=t1.date1 and table1.name=t1.name"
SQL = SQL & " where date1 Is Not Null"
SQL = SQL & " group by table1.name"
它工作正常。
如果您想获得长度大于1的序列
,请尝试此操作SELECT Absence.Racf, Count(Absence.RecordDate) AS CountOfRecordDate
FROM (Absence LEFT JOIN (select Racf, RecordDate+IIf(Weekday([RecordDate],7)=1,2,1) as RecordDate1 from Absence) AS t1 ON (Absence.RecordDate = t1.RecordDate1) AND (Absence.Racf = t1.Racf))
LEFT JOIN (select Racf, [RecordDate]-IIf(Weekday([RecordDate],2)=1,2,1) as RecordDate2 from Absence) AS t2 ON (Absence.RecordDate = t2.RecordDate2) AND (Absence.Racf = t2.Racf)
WHERE (((t1.RecordDate1) Is Not Null) AND ((t2.RecordDate2) Is Null))
GROUP BY Absence.Racf;
或者,如果您想获得一个或多个连续日期的序列
SELECT Absence.Racf, Count(Absence.RecordDate) AS CountOfRecordDate
FROM Absence LEFT JOIN (select Racf, [RecordDate]+IIf(Weekday([RecordDate],7)=1,2,1) as RecordDate2 from Absence) AS t2 ON (Absence.RecordDate = t2.RecordDate2) AND (Absence.Racf = t2.Racf)
WHERE (((t2.RecordDate2) Is Null))
GROUP BY Absence.Racf;
像以前一样添加到SQL字符串。
答案 1 :(得分:0)
这可以使用Excel中的数组公式来完成。在D中我有=INDEX($A2:$A$15,MATCH(0,COUNTIF($D$1:$D1,$A2:$A$15),0))
来获得独特的员工,然后在E中我有以下内容来计算实例=SUM(--(($A$1:$A$15=D1)*(OFFSET($A$1:$A$15,1,0)=D1)*(OFFSET($B$1:$B$15,1,0)-$B$1:$B$15)=1))
,它给出的结果是这样的。您需要根据weekday
添加其他条件(稍后会调整为运行时间较短)这依赖于按日期顺序排列的数据
编辑:我理解这不是完整的答案,需要修改,一个起点:o)
涵盖周日缺勤(仍需要工作日检查):
=D1 & " " & COUNTIF($A$1:$A$15,D1) &" instances " & SUM(--(--($A$1:$A$15=D1)*--(OFFSET($A$1:$A$15,1,0)=D1))*--(--(OFFSET($B$1:$B$15,1,0)-$B$1:$B$15=1)+--(OFFSET($B$1:$B$15,1,0)-$B$1:$B$15=2)))&" Consecutive"
检查工作日
=D2 & " " & COUNTIF($A$1:$A$15,D2) &" instances " & SUM(--(--($A$1:$A$15=D2)*--(OFFSET($A$1:$A$15,1,0)=D2))*--(--(OFFSET($B$1:$B$15,1,0)-$B$1:$B$15=1)+--(WEEKDAY(OFFSET($B$1:$B$15,1,0),2)=1)*((OFFSET($B$1:$B$15,1,0)-$B$1:$B$15=2)))) & " Consecutive"
答案 2 :(得分:0)
基于表000Absence,SQL方法将是一个基础,它是来自EEName和AbsDate示例的数据。
SELECT abs1.EEName, abs1.AbsDate,
(select count(abs2.EEName) from 000Absence as abs2 where abs2.[EEName]=abs1.[EEName]) AS INSTANCES,
(select count(abs3.EEName) from 000Absence as abs3 where abs3.[EEName]=abs1.[EEName] and abs3.[AbsDate]=abs1.[AbsDate]+iif(weekday(abs3.[AbsDate],7)=1,2,1)) AS CONSECUTIVE
FROM 000Absence AS abs1;
可以从查询中获取输出,按Employee等分组。