2011-02-09 40 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