I want to program a function that sees how much time the client spent of time without paying the seller. My problem is that I'm kind of confused because this is the function that already programmed:
ALTER function [dbo].[bal_cli](@date date, @diff1 int,@diff2 int) returns @table1
table
(
solde float,
cpt char(20)
)as
begin
insert into @table1(solde,cpt)
SELECT SUM(a.ECRDEBIT-a.ECRCREDIT) AS solde,a.CPTCODE
FROM dbo.ECRITURES AS a , dbo.ECRITURES AS b
WHERE (a.JORID = '7' or a.JORID = '10' or a.JORID = '22' or a.JORID = '15' or a.JORID = '16' or a.JORID ='5' or a.JORID = '38' or a.JORID = '45' or a.JORID = '12' or a.JORID = '17' or a.JORID = '44' or a.JORID = '18' or a.JORID = '8' or a.JORID = '9') AND (a.ECRISMARQUE <> 'O') AND (a.CPTCODE LIKE '3421%') and
( DATEDIFF(day, a.ECRDATEEFFET, @date) >= @diff1 and DATEDIFF(day, a.ECRDATEEFFET, @date) < @diff2) and (YEAR(a.ECRDATEEFFET) = YEAR(@date))
GROUP BY a.CPTCODE
return;
end;
My issue is that I want JUST for JORID to calculate the difference note with ECRDATEEFFET but with ECRLIBELLE.