WoodyGuthrie
MIS
Hello Everyone! I'll try a slightly different approach here at Tek-Tips.
I'm dealing with employee attendance data. An employee may receive amnesty for infractions that have occurred in the past (like unapproved absences etc.) I have an amnesty table that tracks the infraction(s) for which the amnesty was given. So in the amnesty table (tblAmnestyParentChild), the guidAttendanceCodeChildrenID points back to the original infraction in the attendance codes table (tblAssociateAttendanceCodes).
Although each attendance item in the tblAssociateAttendanceCodes (approved, unapproved, amnesty, etc) has its own associated date, I need to return the date of the original infraction (say the unapproved attendance date) with the row that indicates the amnesty and not the date that the amnesty code was given.
Sample data:
Current Result Set
guidAmnesty guidAttendanceCode AttendanceCode Date
NULL 7A0A970A5CD4 Approved 2008-06-05
NULL 9BF2F1986ED9 Unapproved 2008-06-05
NULL 27058A22E167 Approved 2008-06-06
NULL 9C77054505CE Unapproved 2008-06-09
NULL DDC7ED8C2284 Approved 2008-06-09
NULL 975628B59CC3 Approved 2008-06-10
NULL 64A11AB65DB9 Unapproved 2008-06-11
NULL 837146E30E7B Unapproved 2008-06-12
837146E30E7B 648EE650E22E Amnesty 2008-09-11
64A11AB65DB9 C3F5D3C29599 Amnesty 2008-12-11
What I want to return is like above except for the last two rows (the 2 Amnesty rows) with the date from their matching guidAttendanceCode row as follows:
837146E30E7B 648EE650E22E Amnesty 2008-06-12
64A11AB65DB9 C3F5D3C29599 Amnesty 2008-06-11
All Other Rows would be as they are.
I was thinking that I could use a CTE to result all Amnesty rows with the required date (the date of the original infraction) and then join that to the attendance codes table and that does works except when there are multiple amnesty rows, I get the subquery returned more than 1 value warning. So I'm really not gaining anything from the CTE as I thought.
I hope I've explained this good enough. Any help is greatly appreciated.
Thanks.
Woody
I'm dealing with employee attendance data. An employee may receive amnesty for infractions that have occurred in the past (like unapproved absences etc.) I have an amnesty table that tracks the infraction(s) for which the amnesty was given. So in the amnesty table (tblAmnestyParentChild), the guidAttendanceCodeChildrenID points back to the original infraction in the attendance codes table (tblAssociateAttendanceCodes).
Although each attendance item in the tblAssociateAttendanceCodes (approved, unapproved, amnesty, etc) has its own associated date, I need to return the date of the original infraction (say the unapproved attendance date) with the row that indicates the amnesty and not the date that the amnesty code was given.
Sample data:
Current Result Set
guidAmnesty guidAttendanceCode AttendanceCode Date
NULL 7A0A970A5CD4 Approved 2008-06-05
NULL 9BF2F1986ED9 Unapproved 2008-06-05
NULL 27058A22E167 Approved 2008-06-06
NULL 9C77054505CE Unapproved 2008-06-09
NULL DDC7ED8C2284 Approved 2008-06-09
NULL 975628B59CC3 Approved 2008-06-10
NULL 64A11AB65DB9 Unapproved 2008-06-11
NULL 837146E30E7B Unapproved 2008-06-12
837146E30E7B 648EE650E22E Amnesty 2008-09-11
64A11AB65DB9 C3F5D3C29599 Amnesty 2008-12-11
What I want to return is like above except for the last two rows (the 2 Amnesty rows) with the date from their matching guidAttendanceCode row as follows:
837146E30E7B 648EE650E22E Amnesty 2008-06-12
64A11AB65DB9 C3F5D3C29599 Amnesty 2008-06-11
All Other Rows would be as they are.
I was thinking that I could use a CTE to result all Amnesty rows with the required date (the date of the original infraction) and then join that to the attendance codes table and that does works except when there are multiple amnesty rows, I get the subquery returned more than 1 value warning. So I'm really not gaining anything from the CTE as I thought.
I hope I've explained this good enough. Any help is greatly appreciated.
Thanks.
Code:
WITH InfractionDates (guidAssociateAttendanceCodeID, dteAttendanceCode, strAssociateID)
AS
(SELECT ac.guidAssociateAttendanceCodeID, ac.dteAttendanceCode, ad.strAssociateID
FROM dbo.tblAssociateDemographics ad
INNER JOIN dbo.tblAssociateAttendanceCodes ac
ON ad.guidAssociateID=ac.guidAssociateID
WHERE ac.guidAssociateAttendanceCodeID in
(SELECT apc.guidAttendanceCodeChildrenID
FROM dbo.tblAmnestyParentChild apc)
)
SELECT
apc.guidAttendanceCodeChildrenID
, ac.guidAssociateAttendanceCodeID
, ad.strAssociateID
, ac.dblScore
, aci.strAttendanceItem
, CASE WHEN guidAttendanceCodeChildrenID IS NOT NULL
THEN (SELECT ID.dteAttendanceCode
FROM InfractionDates ID
JOIN dbo.tblAssociateAttendanceCodes ac
ON ID.guidAssociateAttendanceCodeID = ac.guidAssociateAttendanceCodeID
)
ELSE ac.dteAttendanceCode
END AS dteAttendanceCode
, ad.strLastName
, ad.strFirstName
, ad.strDetailsStatus
, ad.dteDetailsHireDate
, ac.dblAdjScore
, ac.strAdjType
, d.guidDepartmentID
, f.strName
, d.strName
, ad.dblAttendanceScore
, ac.dteInsertedOn
, ac.strAdjComment
FROM
dbo.tblAssociateDemographics ad
INNER JOIN dbo.tblAssociateAttendanceCodes ac
ON ad.guidAssociateID=ac.guidAssociateID
INNER JOIN dbo.tblDepartments d
ON ad.guidDetailsHomeDepartment=d.guidDepartmentID
INNER JOIN dbo.tblFacilities f
ON ad.guidDetailsHomeFacility=f.guidFacilityID
LEFT OUTER JOIN dbo.tblAttendanceCodeItems aci
ON ac.intAttendanceItemCode=aci.intAttendanceItemCode
LEFT JOIN dbo.tblAmnestyParentChild apc
ON apc.guidAttendanceCodeID = ac.guidAssociateAttendanceCodeID
Woody