简体   繁体   English

计数/组T-SQL

[英]Count/Group T-SQL

Hello The following is a sample data. 您好,以下是示例数据。

DateGroupID     StartDate       EndDate
1               2013-01-01      2013-01-07
2               2013-01-08      2013-01-14
3               2013-01-15      2013-01-21
.
.
.
15              2013-04-01      2013-04-07

EMPID   GroupID     JoinDate            TerminationDate
1       A           2013-01-01          2013-03-24
2       B           2013-01-05          NULL
3       C           2013-01-05          NULL
4       A           2013-01-05          2013-03-20
5       B           2013-01-17          NULL
6       D           2013-02-01          NULL
7       A           2013-02-24          NULL
8       A           2013-02-28          NULL
9       B           2013-03-02          NULL
10      B           2013-03-12          NULL
11      C           2013-03-22          NULL
12      C           2013-03-22          NULL
13      D           2013-03-26          NULL
14      D           2013-03-29          NULL
15      A           2013-04-01          NULL

I am trying to get count for employees who is ACTIVE on each day and group it by GroupID based on which DateGroupID I select. 我试图获取每天处于活动状态的员工的人数,并根据我选择的DateGroupID按GroupID对其进行分组。

So for example, If I select DateGroupID = 1 (in WHERe clause I would assume), I want to get count of ACTIVE users for each day between StartDate and EndDate. 因此,例如,如果我选择DateGroupID = 1(我假设在WHERe子句中),我想获取StartDate和EndDate之间每天的活跃用户数。 So my output should be like 所以我的输出应该像

GROUPID     COUNT       Date
A           1           2013-01-01  (1 EMP was added to this group on this day)
B           0           2013-01-01  (NO Emp for this group were active on this day)
C           0           2013-01-01  (NO Emp for this group were active on this day)
D           0           2013-01-01  (NO Emp for this group were active on this day)

A           1           2013-01-02  (NO Emp for this group were added but 1 is active from the past)
B           0           2013-01-02  (NO Emp for this group were active on this day)
C           0           2013-01-02  (NO Emp for this group were active on this day)
D           0           2013-01-02  (NO Emp for this group were active on this day)

A           1           2013-01-03  (NO Emp for this group were added but 1 is active from the past)
B           0           2013-01-03  (NO Emp for this group were active on this day)
C           0           2013-01-03  (NO Emp for this group were active on this day)
D           0           2013-01-03  (NO Emp for this group were active on this day)

A           1           2013-01-04  (NO Emp for this group were added but 1 is active from the past)
B           0           2013-01-04  (NO Emp for this group were active on this day)
C           0           2013-01-04  (NO Emp for this group were active on this day)
D           0           2013-01-04  (NO Emp for this group were active on this day)


A           2           2013-01-05 (1 more Emp was added to this group on this day)
B           1           2013-01-05 (1 EMP was added to this group on this day)
C           1           2013-01-05 (1 EMP was added to this group on this day)
D           0           2013-01-05 (NO Emp for this group were active on this day)
.
.
.
.
A           2           2013-01-17 (2 EMP active on this day for this group)
B           2           2013-01-17 (1 more Emp was added to this group on this day))
C           1           2013-01-17 (NO Emp for this group were added but 1 is active from the past)
D           0           2013-01-17 (NO Emp for this group were active on this day)
.
.
.
A           2           2013-03-24 (2 EMP were removed and added as for this day, 2 active EMP)
B           4           2013-03-24 (So far 4 active EMP for this group)
C           3           2013-03-24 (So Far 3 active EMP for this group)
D           2           2013-03-24 (So far 2 active EMP for this group)

OR in better view 或以更好的视角

WHEN I SELECT DateGoupID = 3 当我选择DateGoupID = 3时

GroupID     2013-01-15      2013-01-16      2013-01-17      2013-01-18      2013-01-19      2013-01-20      2013-01-21
A               2               2               2               2               2               2               2
B               1               1               2               2               2               2               2
C               1               1               1               1               1               1               1
D               0               0               0               0               0               0               0

Do you need the DateGroup table? 您是否需要DateGroup表?

If not: 如果不:

     Select GroupID, Count(EmpId), JoinDate
     from dbo.[EmployeeStartDateTableName]
     group by GroupID, JoinDate

If So: 如果是这样的话:

     SELECT   GroupID, Count(EmpId), JoinDate,   
     FROM         dbo.[EmployeeStartDateTableName] INNER JOIN
                  dbo.[DateGroupTableName] ON [EmployeeStartDateTableName].JoinDate =
     dbo.[DateGroupTableName].StartDate
     Where groupID = [InsertGroupId]
     group by GroupID, JoinDate

Then if you want the critera to be the DateGroupID 然后,如果您希望critera成为DateGroupID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM