Clear[Evaluate[Context[]<>"*"]];
(*xySum[n_]:=Block[{i},Sum[x[i]y[i],{i,1,n}]];
xSum[n_]:=Block[{i},Sum[x[i],{i,1,n}]];
ySum[n_]:=Block[{i},Sum[y[i],{i,1,n}]];
xxSum[n_]:=Block[{i},Sum[x[i]^2,{i,1,n}]];
yySum[n_]:=Block[{i},Sum[y[i]^2,{i,1,n}]];
xkSum[n_]:=Block[{i},Sum[x[i]k[i],{i,1,n}]];
kSum[n_]:=Block[{i},Sum[k[i],{i,1,n}]];
ykSum[n_]:=Block[{i},Sum[y[i]k[i],{i,1,n}]];
kkSum[n_]:=Block[{i},Sum[k[i]^2,{i,1,n}]];*)
a[x_,y_,k_,n_]:=(n xySum[n]+n  xkSum[n]-xSum[n]ySum[n]-xSum[n]kSum[n])/(n xxSum[n]-xSum[n]^2);
diffSummand[x_,y_,k_,i_,n_]:=(a[x,y,k,n] x[i]-y[i]-k[i]Δ)^2;
diff[x_,y_,k_,n_]:=Block[{i},Sum[diffSummand[x,y,k,i,n],{i,1,n}]];
a[x,y,k,n]
(*Collect[Expand@diffSummand[x,y,k,Δ,i,n],k[i]]*)
zwei=kkSum[n];
eins=2xkSum[n](-n xkSum[n]+kSum[n]xSum[n]-n xySum[n]+xSum[n]ySum[n])/(n xxSum[n]-xSum[n]^2)+2ykSum[n];
null=(n^2xxSum[n]xkSum[n]^2-2n kSum[n]xxSum[n]xkSum[n]xSum[n]+kSum[n]^2xxSum[n]xSum[n]^2+2n^2xxSum[n]xkSum[n]xySum[n]-2n kSum[n]xxSum[n]xSum[n]xySum[n]+n^2xxSum[n]xySum[n]^2-2n xxSum[n]xkSum[n]xSum[n]ySum[n]+2kSum[n] xxSum[n]xSum[n]^2ySum[n]-2n xxSum[n]xSum[n]xySum[n]ySum[n]+xxSum[n]xSum[n]^2ySum[n]^2)/(n xxSum[n]-xSum[n]^2)^2+(-2n xySum[n]xkSum[n]+2kSum[n]xySum[n]xSum[n]-2n xySum[n]^2+2xySum[n]xSum[n]ySum[n])/(n xxSum[n]-xSum[n]^2)+yySum[n];
Collect[zwei+eins+null//FullSimplify,{kSum[n],kkSum[n],xkSum[n],ykSum[n]}]
Dkj[term_]:=D[term,kkSum[n]]2k[j]+D[term,kSum[n]]+D[term,xkSum[n]]x[j]+D[term,ykSum[n]]y[j];
Collect[Dkj[null+eins+zwei//FullSimplify],{k[j],kSum[n],kkSum[n],xkSum[n],ykSum[n]}]