* DISTINCT AND GROUP ARE NOT OPTIMIZABLE SELECT DISTINCT Deptt FROM EmpSalary GROUP BY 1 INTO CURSOR TEMP * INDEX on Deptt TAG dtag && now index is usefull SELECT TOP 2 Emp_ID ,Emp_Name ,Salary ,Deptt FROM EmpSalary; WHERE _TALLY>0 AND Deptt=TEMP.Deptt ORDER BY 3 DESC; INTO CURSOR Result READWRITE SELECT TEMP SKIP SIGN(_TALLY) SCAN REST INSERT INTO Result; SELECT TOP 2 Emp_ID ,Emp_Name ,Salary ,Deptt FROM EmpSalary; WHERE Deptt=TEMP.Deptt ORDER BY 3 DESC ENDSCAN USE SELECT Result