I'm using SQL Server 2005
I need to gather data from 3 tables, but 1 table has a 1:many relationship with the main table. From that table, I only want the first record for that case and stage. This is what I was trying (and failing):
Ultimately, I need to calculate the Responded & the Solved time (from the created date) and get an average for each by Company. Because each case can have multiple Responded and Solved stages, I need to get the first occurence of each per case. If I can't do it by using Distinct, maybe I could also create a new column to defines when each is the first for that case and then filter on it?
Any suggestions would be greatly appreciated.
I need to gather data from 3 tables, but 1 table has a 1:many relationship with the main table. From that table, I only want the first record for that case and stage. This is what I was trying (and failing):
Code:
SELECT Cases.Case_ReferenceID, Company.Comp_Name, Cases.Case_Description, Cases.Case_CreatedDate, c1.Case_Stage, c1.Case_Timestamp
FROM CaseProgress c1
INNER JOIN (SELECT DISTINCT CaseProgress.Case_CaseID, CaseProgress.Case_Stage from CaseProgress) c2 ON c1.Case_CaseID = c2.Case_CaseID -- AND c1.Case_Stage = c2.Case_Stage
INNER JOIN Cases ON Cases.Case_CaseID=c1.Case_CaseID
INNER JOIN Company ON Cases.Case_PrimaryCompanyID=Company.Comp_CompanyID
WHERE c1.Case_Stage IN ('Responded', 'Solved')
AND Cases.Case_CreatedDate > '9/20/2013 12:00:00 AM'
ORDER BY Cases.Case_ReferenceID, c1.Case_Timestamp
Ultimately, I need to calculate the Responded & the Solved time (from the created date) and get an average for each by Company. Because each case can have multiple Responded and Solved stages, I need to get the first occurence of each per case. If I can't do it by using Distinct, maybe I could also create a new column to defines when each is the first for that case and then filter on it?
Any suggestions would be greatly appreciated.