3

这是表结构:

tbl申请人

applicantID (index)   |     ApplyingForYear (nvarchar) 
------------------------------------------------------
1                           2013/14
11                          2013/14
13                          2013/14
12                          2013/14
15                          2013/14
21                          2012/13

tblApplicantSchools_shadow

id (index)   |    applicantID    |    updated (datetime)   |   statusID (int)   |   schoolID (int)
-----------------------------------------------------------------------------------------------------
1                   11                  2012-09-24 00:00:00.000     3                   2
1                   13                  2012-10-24 00:00:00.000     4                   2
2                   15                  2012-11-24 00:00:00.000     3                   4
3                   13                  2012-03-24 00:00:00.000     4                   3
4                   12                  2012-09-24 00:00:00.000     4                   1
5                   21                  2012-11-03 00:00:00.000     5                   2
6                   11                  2012-09-04 00:00:00.000     4                   4

我需要做的是:

  • 获取所有具有ApplyingForYear“2013/14”的申请人tblApplicants
  • 有一个statusID4
  • 我只想数一次——即使它们出现两次或更多tblApplicantschools_show
  • 对不同申请人的数量进行分组(如上所述) - 按updated日期列(按周分组)

所以根据上面的示例数据,应该有 3 行出来,(因为ApplicantID13 出现了两次,我只想要他一次)。

结果应该是这样的:

    Datesubmitted              TotalAppsPerWeek
    -------------------------------------------------------
    2012-10-24 00:00:00.000    1
    2012-09-24 00:00:00.000    1
    2012-09-04 00:00:00.000    1

这是我到目前为止所拥有的 - 但它会产生 4 行,而不是 3 :(

select  
   DATEADD(ww,(DATEDIFF(ww,0,[tblApplicantSchools_shadow].updated)),0) AS Datesubmitted,
   count(DISTINCT [tblApplicantSchools_shadow].applicantID) as TotalAppsPerWeek 
FROM tblApplicants  
INNER JOIN tblApplicantSchools_shadow 
      ON tblApplicantS.ApplicantID = tblApplicantSchools_shadow.applicantID
WHERE 
     ApplyingForYear = '2013/14' 
     AND [tblApplicantSchools_shadow].statusID = 4
GROUP BY 
     DATEADD(ww, (DATEDIFF(ww, 0, [tblApplicantSchools_shadow].updated)), 0)

这是一个小提琴:http ://sqlfiddle.com/#!3/3aa61/42

4

1 回答 1

3

根据您的标题,我假设您希望每个申请人的一行是最小的一行id。您可以使用以下功能为每个申请人 ID 选择一行ROW_NUMBER()

;with latestApplication AS
(
    SELECT DATEADD(ww,(DATEDIFF(ww,0,[tblApplicantSchools_shadow].updated)),0) 
               AS Datesubmitted,
        [tblApplicantSchools_shadow].applicantID,
        ROW_NUMBER() OVER (PARTITION BY [tblApplicantSchools_shadow].applicantID 
                           ORDER BY [tblApplicantSchools_shadow].id) 
            AS rn
    FROM tblApplicants  
    INNER JOIN tblApplicantSchools_shadow 
        ON tblApplicantS.ApplicantID = tblApplicantSchools_shadow.applicantID
    WHERE ApplyingForYear = '2013/14' 
    AND [tblApplicantSchools_shadow].statusID = 4
)
select Datesubmitted, COUNT(1) AS TotalAppsPerWeek
FROM latestApplication
WHERE rn = 1
group by Datesubmitted
order by Datesubmitted DESC

http://sqlfiddle.com/#!3/3aa61/57

于 2013-03-13T17:37:32.973 回答