Hi Moug,
Based on your execution stats, the "Department" table is the most problematic.
And indeed, based on your query, its implementation looks very... Bizarre.
On the one hand, you use cross join in order to get the data from Department and from Shift. This creates a Cartesian Product - i.e. "everything times everything".
On the other hand, there's only one possible instance where you actually make use of the Department table, and that's based on the following code snippet:
(case d.ShiftID when 1 then c.name when '2' then 'Adimin' when '3' then 'security' else 'closed' end)
In other words, the only time when you use the Department table is when ShiftID in the Shift table equals to 1. In all other cases, the Department table is completely ignored.
Unfortunately, since CASE in SQLServer doesn't help much with the optimizer, this doesn't help reduce the actual number of results returned.
What I would suggest, in this case, is to have a right join between "Department" and "Shift", using the condition: d.ShiftID = 1. i.e., return data from Department only if Shift.ShiftID=1. (the CASE can remain as it is now)
Like so:
select a.name , a.productNumber ,b.StandardPrice ,d.name as shift , (case d.ShiftID when 1 then c.name when '2' then 'Adimin' when '3' then 'security' else 'closed' end) Deparment from production.product a inner join Purchasing.ProductVendor b on a.ProductID = b.ProductID , humanresources.Department c RIGHT JOIN humanresources.Shift d ON d.ShiftID = 1Also, I don't quite understand why would you want a Cross Join between all your departments and all your shifts and all your products. Can you please exp


