#Parameter1:Anzahl Monate:Table:10000;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23
#Parameter2:Behandler:DataTable:select name+' '+vorname from privat where behandler=1 AND aktiv=1 order by nrprivat
-SQL-
declare @nrbehandler int
if '#Parameter2'<>'' begin
   select @nrbehandler=isnull(nrprivat,-2) from privat where behandler=1 and '#Parameter2'=dbo.privat.name+' '+dbo.privat.vorname
end else begin
   set @nrbehandler=-1
end
SELECT        TOP (100) PERCENT SUM(Total) AS Total, DATEDIFF(MM, datum, GETDATE()) AS Anz_Monate, YEAR(datum) AS Jahr, MONTH(datum) AS Monat, LTRIM(STR(YEAR(datum))) + '.' + LTRIM(STR(MONTH(datum))) AS Expr1
FROM            dbo.Faktura
WHERE        (Status <> 9)  and (nrbehandler=@nrbehandler or @nrbehandler=-1)
GROUP BY DATEDIFF(MM, datum, GETDATE()), YEAR(datum), MONTH(datum), LTRIM(STR(YEAR(datum))) + '.' + LTRIM(STR(MONTH(datum)))
HAVING        (DATEDIFF(MM, datum, GETDATE()) <= #Parameter1 - 1) 
ORDER BY Jahr, Monat


