I have created the following function:
CREATE FUNCTION ufn_sales_stats ( @compid int, @target_date date ) RETURNS TABLE AS RETURN ( SELECT