Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Condition a Join - if possible

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2010-02-19 : 14:48:03
I need to expand a query and not sure how to do this. I need to condition a Join bases upon value of a parameter

Code:

@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 Job
inner join [Event] on dbo.Event.Jobguid = dbo.Job.Jobguid
inner join dbo.EventStatusType on dbo.EventStatusType.EventStatusTypeGuid = dbo.Event.EventStatus
inner join Item on dbo.Item.Itemguid = dbo.Event.Itemguid
inner join dbo.EmployeeLaborEvent on dbo.EmployeeLaborEvent.EventGuid = dbo.Event.EventGuid
inner join dbo.Employee on dbo.Employee.EmployeeGuid = dbo.EmployeeLaborEvent.EmployeeGuid
inner join dbo.JobCraft on dbo.JobCraft.JobCraftGuid = dbo.EmployeeLaborEvent.JobCraftGuid
inner join dbo.Batch on dbo.Batch.Batchguid = dbo.Event.Batchguid

Here 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.EmployeeLaborAttributeGuid

only 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.EmployeeGuid


WHERE 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)

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-02-19 : 15:07:09
All you should need to do is add the condition to your join:
AND @HoursType = 'ALL'
Go to Top of Page
   

- Advertisement -