I need to expand a query and not sure how to do this. I need to condition a Join bases upon value of a parameterCode:@JobNumber int,@ItemNumber char(20),@DateFrom datetime = Null,@DateTo datetime = Null,@EmployeeName varchar(15) = Null,&HoursType char(3) SELECT dbo.Batch.ReportDate as job_date, dbo.Item.CompanyItemId as cost_code, pay_type = '40', pay_text = 'Regular Time', substring(dbo.Employee.Name, 1, 14) as employee_name, substring(dbo.JobCraft.Name, 1, 15) as job_craft, dbo.EmployeeLaborEvent.Hours labor_hours, e.Start, e.[Stop], e.Lunch, e.Rest, substring(dbo.EventStatusType.Name, 1, 1) as event_name FROM Jobinner join [Event] on dbo.Event.Jobguid = dbo.Job.Jobguidinner join dbo.EventStatusType on dbo.EventStatusType.EventStatusTypeGuid = dbo.Event.EventStatusinner join Item on dbo.Item.Itemguid = dbo.Event.Itemguidinner join dbo.EmployeeLaborEvent on dbo.EmployeeLaborEvent.EventGuid = dbo.Event.EventGuid inner join dbo.Employee on dbo.Employee.EmployeeGuid = dbo.EmployeeLaborEvent.EmployeeGuidinner join dbo.JobCraft on dbo.JobCraft.JobCraftGuid = dbo.EmployeeLaborEvent.JobCraftGuidinner join dbo.Batch on dbo.Batch.Batchguid = dbo.Event.BatchguidHere I need to insert and execute:left join dbo.EmployeeGroup on dbo.EmployeeGroup.EmployeeGroupId = dbo.EmployeeLaborEvent.EmployeeGroup left join dbo.EmployeeLaborAttribute on dbo.EmployeeLaborAttribute.EmployeeLaborAttributeGuid = dbo.EmployeeLaborEvent.EmployeeLaborAttributeGuidonly if parameter @HoursType = "ALL"LEFT JOIN ( SELECT e.BatchGuid, ete.EmployeeGuid, MAX(CASE etet.Name WHEN 'Start' THEN ete.Time ELSE NULL END) AS Start, MAX(CASE etet.Name WHEN 'Stop' THEN ete.Time ELSE NULL END) AS [Stop], MAX(CASE etet.Name WHEN 'Lunch' THEN ete.Time ELSE NULL END) AS Lunch, MAX(CASE etet.Name WHEN 'Break' THEN ete.Time ELSE NULL END) AS Rest FROM dbo.Event AS e INNER JOIN EmployeeTimeEvent AS ete ON ete.EventGuid = e.EventGuid --and ete.EmployeeGuid = emp.EmployeeGuid INNER JOIN EmployeeLaborEvent as emp on emp.EmployeeGuid = ete.EmployeeGuid INNER JOIN EmployeeTimeEventType AS etet ON etet.EmployeeTimeEventTypeGuid = ete.EmployeeTimeEventTypeGuid GROUP BY e.BatchGuid, ete.EmployeeGuid ) AS e ON e.BatchGuid = dbo.Batch.BatchGuid AND e.EmployeeGuid = dbo.EmployeeLaborEvent.EmployeeGuidWHERE dbo.Job.CompanyJobId = @JobNumber and dbo.Item.CompanyItemId = @ItemNumber and ( @DateFrom IS Null OR dbo.Batch.Reportdate >= @DateFrom) and (@DateTo IS Null OR dbo.Batch.ReportDate <= @DateTo) and dbo.EmployeeLaborEvent.Hours <> 0 and (dbo.Employee.Name LIKE @EmployeeName + '%' OR NULLIF(@EmployeeName,'') IS NULL)