Have following query below. I only need to execute the line "HAVING SUM(dbo.EquipmentLaborEvent.Hours) < @Hours)" if @Hours being passed in is not zero. How would I do that? Thank you.
@Hours INT
.
..
...
SELECT dbo.Equipment.CompanyEquipmentID,SUM(dbo.EquipmentLaborEvent.Hours) AS hours
FROM dbo.Equipment
inner join dbo.EquipmentLaborEvent on dbo.EquipmentLaborEvent.EquipmentGuid = dbo.Equipment.EquipmentGuid
inner join dbo.Event on dbo.Event.EventGuid = dbo.EquipmentLaborEvent.EventGuid
inner join dbo.Batch on dbo.Batch.BatchGuid = dbo.Event.BatchGuid
WHERE (@DateFrom IS Null or dbo.Batch.Reportdate >= @DateFrom) and
(@DateTo IS Null or dbo.Batch.ReportDate <= @DateTo)
GROUP BY dbo.Equipment.CompanyEquipmentID
HAVING SUM(dbo.EquipmentLaborEvent.Hours) < @Hours)
@Hours INT
.
..
...
SELECT dbo.Equipment.CompanyEquipmentID,SUM(dbo.EquipmentLaborEvent.Hours) AS hours
FROM dbo.Equipment
inner join dbo.EquipmentLaborEvent on dbo.EquipmentLaborEvent.EquipmentGuid = dbo.Equipment.EquipmentGuid
inner join dbo.Event on dbo.Event.EventGuid = dbo.EquipmentLaborEvent.EventGuid
inner join dbo.Batch on dbo.Batch.BatchGuid = dbo.Event.BatchGuid
WHERE (@DateFrom IS Null or dbo.Batch.Reportdate >= @DateFrom) and
(@DateTo IS Null or dbo.Batch.ReportDate <= @DateTo)
GROUP BY dbo.Equipment.CompanyEquipmentID
HAVING SUM(dbo.EquipmentLaborEvent.Hours) < @Hours)