This is my SQL-query:
SELECT
b.MaakArtikel,
b.Bewerking,
[pp].dbo.WORKINGDAYADD('2013-06-27 00:00:00.000',0-b.Startdag_backwards) AS Startdatum,
i.Class_06 AS Afdeling,
b.Minuten*10+ISNULL(br.Tijd,0) AS Minuten,
1+ISNULL(br.Orders,0) AS Aantal
FROM [pp].dbo.VW_BEWERKINGSTRUCTUUR b
LEFT OUTER JOIN [211].dbo.Items i
ON b.MaakArtikel = i.ItemCode
LEFT OUTER JOIN [pp].dbo.VW_BEZETTING_RAW br
ON [pp].dbo.WORKINGDAYADD('2013-06-27 00:00:00.000',0-b.Startdag_backwards) = br.Start
AND i.Class_06 = br.Afdeling
WHERE MaakArtikel = 'HT 10.038'
The query works properly, but it's a little bit slow. That's because of my second OUTER JOIN
. I have to join the view by Startdatum
(that's selected at line 4). As it is not a real column name, I can't use it directly in my OUTER JOIN
. That means that the function [pp].dbo.WORKINGDAYADD()
has to be triggered twice (once in my selection which is not a problem, and once in my OUTER JOIN
which is double work).
I could write a stored procedure and use the result of function [pp].dbo.WORKINGDAYADD()
in a variable, but that is not disireable. Is there a way to use Startdatum in my OUTER JOIN
expression? Or do I really have to use a stored procedure for this?