sql server - I am learning SQL and work for a school system -
my quest 1 line per employmentassignment row if staff member has multiple rows in employment table. (include fields existing staffmember view come person, identity, employmentassignment, school , department. include new field alias "districtstart" reporting person's earliest employment.startdate.) question exam , can not figure out how make pull 1 assignment. have , says correct but... pulling more 1 assignment. keep me banging head against wall anymore.
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) 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 dbo.person p (nolock) inner join dbo.[identity] (nolock) on p.currentidentityid = i.identityid inner join dbo.employment e on e.personid = p.personid inner join dbo.employmentassignment ea (nolock) on p.personid = ea.personid inner join dbo.school s (nolock) on s.schoolid = ea.schoolid left outer join dbo.department d (nolock) on d.departmentid = ea.departmentid group 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
you need take out e.startdate group statement. in query taking min(e.startdate), won't if grouping it. here trivial example show mean:
select '20110101' startdate #datesexample union select '20110102' startdate union select '20110103' startdate select min(startdate) #datesexample group startdate select min(startdate) #datesexample
Comments
Post a Comment