• Personally I prefer an inline TVF for my 'parameterized views', like so

    CREATE FUNCTION tvf_report

    (

    -- Add the parameters for the function here

    @ShiftDepartment varchar(100),

    @Date1 datetime,

    @Date2 datetime

    )

    RETURNS TABLE

    AS

    RETURN

    (

    select VwQPPMaterialRptPart1.Material, VwQPPMaterialRptPart1.ShiftDepartment, VwQPPMaterialRptPart1.QPPMachine,

    VwQPPMaterialRptPart1.ShiftAmount, CASE WHEN VwQPPMaterialRptPart2.Defects IS NULL

    THEN 0 ELSE VwQPPMaterialRptPart2.Defects END AS Defects, VwQPPMaterialRptPart1.MaterialButton, VwQPPMaterialRptPart1.Operation,

    VwQPPMaterialRptPart1.Date from

    (

    SELECT dbo.SAPMaterials.Material, dbo.Shift.ShiftDepartment, REPLACE(REPLACE(dbo.Shift.ShiftMachine, CHAR(13), ''), CHAR(10), '') AS QPPMachine,

    dbo.Shift.ShiftAmount, CONVERT(datetime, LEFT(dbo.Shift.ShiftStart, 10), 103) AS Date, dbo.SAPMaterials.MaterialButton, dbo.SAPMaterials.Operation

    FROM dbo.Shift INNER JOIN

    dbo.SAPMaterials ON dbo.Shift.ShiftDepartment = dbo.SAPMaterials.Department

    AND REPLACE(REPLACE(dbo.Shift.ShiftMachine, CHAR(13), ''), CHAR(10), '') = dbo.SAPMaterials.Process) VwQPPMaterialRptPart1

    left join

    (SELECT ShiftDepartment, REPLACE(REPLACE(ShiftMachine, CHAR(13), ''), CHAR(10), '') AS ShiftMachine, MIN(ShiftAmount) AS ShiftAmounts, SUM(Amount)

    AS Defects, CONVERT(datetime, LEFT(ShiftStart, 10), 103) AS Date, ClassType, Material

    FROM dbo.VwWeeklyPerformance

    GROUP BY ShiftDepartment, REPLACE(REPLACE(ShiftMachine, CHAR(13), ''), CHAR(10), ''), CONVERT(datetime, LEFT(ShiftStart, 10), 103), ClassType, Material

    ) VwQPPMaterialRptPart2

    ON VwQPPMaterialRptPart1.MaterialButton = VwQPPMaterialRptPart2.Material AND

    VwQPPMaterialRptPart1.QPPMachine = VwQPPMaterialRptPart2.ShiftMachine

    where VwQPPMaterialRptPart1.Date between @Date1 and @Date2 and VwQPPMaterialRptPart1.ShiftDepartment =@ShiftDepartment and

    VwQPPMaterialRptPart2.Date between @Date1 and @Date2 and VwQPPMaterialRptPart2.ShiftDepartment =@ShiftDepartment

    )

    but you could make another view with what's inside the TVF. All you need to do to be able to filter the second query on shiftdepartment and date is to return those fields in the result set and don't select them in the select statement you use within the report. It's all that TOPing and ORDERing that's slowing you down.