2011-02-09 88 views
0

我的追求是这个,让每EmploymentAssignment行只有一行即使工作人员在就业表中的多个行。我将(包括来自Person,Identity,EmploymentAssignment,School和Department的现有员工视图中的字段,并包含一个新字段,其别名为“DistrictStart”,报告此人最早的Employment.startDate。)这是我的考试中的一个问题我无法弄清楚如何让它只能完成一项任务。这就是我所拥有的,她说这是正确的,但是......它拉动了多个任务。任何帮助都不会让我的头撞墙。我学习SQL和工作的学校系统

SELECT 
    p.personID, 
    p.stateID, 
    p.staffNumber, 
    p.staffStateID, 
    i.identityID, 
    i.effectiveDate, 
    i.lastName, 
    i.firstName, 
    i.middleName, 
    i.suffix, 
    i.alias, 
    i.gender, 
    i.birthdate, 
    i.ssn, 
    i.raceEthnicity, 
    ea.assignmentID, 
    ea.startDate, 
    MIN(e.startdate) AS DistrictStart, 
    ea.endDate, 
    ea.title, 
    ea.type, 
    ea.teache, 
    ea.specialEd, 
    ea.behavior, 
    ea.health, 
    ea.advisor, 
    ea.supervisor, 
    ea.foodservice, 
    ea.departmentID, 
    s.schoolID, 
    s.name schoolName, 
    s.number schoolNumber, 
    d.name departmentName, 
    ea.excludeReferral, 
    ea.counselor 
FROM  dbo.Person p WITH (NOLOCK) 
    INNER JOIN dbo.[Identity] i WITH (NOLOCK) 
    ON  p.currentIdentityID = i.identityID 
    INNER JOIN dbo.Employment e 
    ON  e.personID = p.personID 
    INNER JOIN dbo.EmploymentAssignment ea WITH (NOLOCK) 
    ON  p.personID = ea.personID 
    INNER JOIN dbo.School s WITH (NOLOCK) 
    ON  s.schoolID = ea.schoolID 
    LEFT OUTER JOIN dbo.Department d WITH (NOLOCK) 
    ON  d.departmentID = ea.departmentID 
GROUP BY e.startdate, 
    p.personID, 
    p.stateID, 
    p.staffNumber, 
    p.staffStateID, 
    i.identityID, 
    i.effectiveDate, 
    i.lastName, 
    i.firstName, 
    i.middleName, 
    i.suffix, 
    i.alias, 
    i.gender, 
    i.birthdate, 
    i.ssn, 
    i.raceEthnicity, 
    ea.assignmentID, 
    ea.startDate, 
    ea.endDate, 
    ea.title, 
    ea.type, 
    ea.teacher, 
    ea.specialEd, 
    ea.behavior, 
    ea.health, 
    ea.advisor, 
    ea.supervisor, 
    ea.foodservice, 
    ea.departmentID, 
    s.schoolID, 
    s.name, 
    s.number, 
    d.name, 
    ea.excludeReferral, 
    ea.counselor 
+2

哎唷!你已经从Access复制出来了吗?也许你应该试着用正确的标点符号和大写字母来问你的问题。 – 2011-02-09 16:49:56

回答

1

您需要从您的group by语句中取出e.startdate。在您的查询中,您正在使用MIN(e.startdate),但如果您也将其分组,则这不会执行任何操作。这里是一个简单的例子,向你展示我的意思:

select '20110101' as StartDate 
into #DatesExample 
union select '20110102' as StartDate 
union select '20110103' as StartDate 

select min(StartDate) 
from #DatesExample 
group by StartDate 

select min(StartDate) 
from #DatesExample