SELECT count(y.sn) as row_num, (x.quantityA-x.quantityB) as VAR, x.sn, x.name, x.quantityA, x.quantityB
FROM TestData as X, TestData as Y
WHERE x.sn >= y.sn
and x.sn >= 2
and y.sn >= 2
GROUP BY x.sn, x.name, x.quantityA, x.quantityB
HAVING count(y.sn) <=3
Note:WHERE条件式 x 与 y 需一致(例:x.sn >= 2 and y.sn >= 2)