I don't think you need domain functions at all for this. I was just looking at what you have with the assumption it's going to do what you want. The only statement in there I thought could be causing the issue was the domain function. Nor do I know why you need to do the average in the query itself. That seems like you could calculate that on any necessary reporting.
If I were to assume this query was correctly pulling the information you wanted:
Code:
PARAMETERS P Short;
SELECT TOP 4 tabPay.CODNOM, tabPay.CODPER, tabPay.Pay
FROM tabPay
GROUP BY tabPay.CODNOM, tabPay.CODPER, tabPay.Pay
HAVING (((tabPay.CODPER)=[P]))
ORDER BY tabPay.CODNOM DESC;
Let's assume for the sake of this example this query is named Q4P
You could get a column stating the average by doing this:
Code:
SELECT *, DAVG([Numberfield]) as MyAvg FROM Q4P
where NUMBERFIELD is the field you want to average. Then your domain function is actually looking at a very small dataset to perform it's function and should be relatively quick (I do not like using domain functions unless I have to because they can be very slow). This would give you a query with your original columns, plus one showing the average for the domain.