CREATE FUNCTION [dbo].[f_DailyIncome] ( @userId int, @date date ) RETURNS decimal(18,2) AS BEGIN declare @income decimal(18,2); select @income = ISNULL(sum(uc.[TotalFee]*bcl.[Percent]),0) from Channel_User as cu left join Back_Purse as bp on bp.OwnerId = cu.LeaderId left join Back_ChannelLevel bcl on cu.ChannelLevelId=bcl.Id left join [Users] as u on cu.ChannelId=u.ChannelId left join [User_Consumptions] as uc on uc.UserId=u.Id where cu.UserId=@userId and uc.[Status]=3 and uc.AddDate=@date; -- Return the result of the function RETURN @income; END GO