MS Access SQL - 工作周期间当前打开的记录数

时间:2017-11-14 15:12:12

标签: sql ms-access ms-access-2010

我有一个包含

的记录集
 unique ID
 open date
 closed date

我想要一个查询来查找每个工作周期间活动的记录数(每年52个工作周)。这些数据跨越了几年,所以工作周不会是独一无二的,每年从1-52重复一次。

一些伪代码看起来像:

 CountIf( ((OpenDate >= Workweek) & (ClosedDate >= WorkWeek)) 
         OR ((OpenDate >= Workweek) & IsNull(ClosedDate)) )

最终结果将是一个表格,其中包含当年/工作周的年份,工作周和活动记录数。

我有一个查询,让我在工作周内开启:

SELECT Year([TABLE].[OpenedDate]) AS [Year], DatePart('ww',TABLE.[OpenedDate])
AS [Work Week], Count(TABLE.[ID]) AS [Count Of IDs Opened]
FROM TABLE
GROUP BY Year([TABLE].[OpenedDate]), DatePart('ww',TABLE.[OpenedDate])
ORDER BY Year([TABLE].[OpenedDate]), DatePart('ww',TABLE.[OpenedDate]);

和一个类似的关闭,但我无法解决如何在每个工作周期间获得积极性。

已编辑添加:

示例数据:

No  Opened Date Closed Date
1   20-Apr-15   23-Apr-15
2   22-Apr-15   04-May-15
3   27-Apr-15   01-May-15
4   29-Apr-15   04-May-15
5   29-Apr-15   05-Jun-15
6   04-May-15   20-May-15
7   04-May-15   07-May-15
8   05-May-15   20-May-15
9   05-May-15   20-May-15
10  06-May-15   20-May-15
11  06-May-15   20-May-15
12  06-May-15   20-May-15
13  07-May-15   20-May-15
14  11-May-15   26-May-15
15  11-May-15   26-May-15
16  13-May-15   26-May-15
17  18-May-15   26-May-15
18  20-May-15   01-Jun-15
19  21-May-15   03-Jun-15
20  12-Oct-15   07-Jul-16
21  02-Nov-15   26-Jul-17
22  06-Nov-15   30-Nov-15
23  09-Nov-15   26-Jul-17
24  08-Feb-17   05-May-17
25  13-Feb-17   
26  13-Feb-17   14-Aug-17
27  07-Mar-17   17-Mar-17

3 个答案:

答案 0 :(得分:1)

您需要一张包含所有年份和周数的表格,我们称之为allYears: {Year, Week}

另外我猜你有一个主表供你记录,我们称之为stores给它一些上下文。否则你会这样做:

SELECT DISTINCT ID FROM `active_stores`

最后,您的有效记录将为active_stores

然后你的查询是:

SELECT allYears.Year,
       stores.ID,
       COUNT([active_stores].ID) as total_active_weeks
FROM allYears, stores
LEFT JOIN active_stores
  ON allYears.Year = Year([active_stores].[OpenedDate])
 AND allYears.Week >= DatePart('ww',[active_stores].[OpenedDate])
 AND allYears.Week <= DatePart('ww',[active_stores].[ClosedDate])
 AND stores.ID = [active_stores].ID
GROUP BY allYears.Year,
         stores.ID

答案 1 :(得分:0)

考虑条件聚合,其中 OpenedDate ClosedDate 的同一周计算活动记录。下面夸大了缩进的可读性。

SELECT Year(t.[OpenedDate]) AS [Year], 
       DatePart('ww', t.[OpenedDate]) AS [Work Week], 

       SUM(IIF(((
                DatePart('ww', t.[OpenedDate]) = [Work Week] AND
                DatePart('ww', t.[ClosedDate]) = [Work Week])
               ) OR
               (
                DatePart('ww', t.[OpenedDate]) = [Work Week] AND
                t.[ClosedDate] IS NULL
               ),
           1,
           0)) AS [Count Of Active IDs]

FROM myTable t
GROUP BY Year(t.[OpenedDate]), DatePart('ww', t.[OpenedDate])
ORDER BY Year(t.[OpenedDate]), DatePart('ww', t.[OpenedDate]);

要进行上述测试,这里是单元级别(非聚合)查询:

SELECT Year(t.[OpenedDate]) AS [Year], 
       DatePart('ww', t.[OpenedDate]) AS [Work Week], 

       t.[OpenedDate],     
       t.[ClosedDate],

       IIF(((
             DatePart('ww', t.[OpenedDate]) = [Work Week] AND
             DatePart('ww', t.[ClosedDate]) = [Work Week])
            ) OR
            (
             DatePart('ww', t.[OpenedDate]) = [Work Week] AND
             t.[ClosedDate] IS NULL
            ),
           1,
           0) AS [Active ID]

FROM myTable t

答案 2 :(得分:0)

所以花了一些时间,但我通过这里发布的解决方案组合解决了我自己的问题。 我用4列制作了表格:

# in the model
before_validation :sort_user_ids
validates :user1_id, uniqueness: { scope: :user2_id }

private

def sort_user_ids
  self.user1_id, self.user2_id = [user1_id, user2_id].sort
end

# in a migration
add_index :table_name, [:user1_id, :user2_id], unique: true

我填写了2010 - 2030年的工作时间,每周工作时间为1-52(或需要时为53),即给定工作周的第一天和给定工作周的最后一天。

然后我运行了这个查询:

tbl_WorkWeeks
 cYear
 cWorkWeek
 FirstDay
 LastDay

感谢大家的帮助。