alter function dbo.F_T_GetRemindOfKalaSerial(@barIdExpect as integer =0, @strKsId as nvarchar(max), @EndDate as integer=0) returns table return ( select x.KS_ID ID, cast(isnull(sum(x.Plus),0) as int) Plus, cast(isnull(sum(x.Minus),0) as int) Minus, cast(isnull(sum(x.Plus),0) - isnull(sum(x.Minus),0) as int) Remind from( select bdp.KS_ID, case when b.Dom_ID_Noe in(29,31,791,1162,1276) then 1 else 0 end Plus, case when b.Dom_ID_Noe in(28,32,792,1163,1275) then 1 else 0 end Minus from Bargeh b inner join BargehDetail bd on b.ID = bd.Bar_ID inner join BargehDetailProperty bdp on bd.ID = bdp.BD_ID and bdp.Dom_ID_Type = 1317 where b.ID != @barIdExpect and (@strKsId = '' or bdp.KS_ID in (select * from dbo.CastStringToTable(@strKsId)) ) and (@EndDate = 0 or b.tarikh <= @EndDate) )x group by x.KS_ID )