SELECT TOP 25 customer
FROM tt
WHERE DocNum LIKE '%something%' AND U_NAME NOT LIKE '%something%'
GROUP BY customer
ORDER BY MIN(DocNum) DESC
Let's say I have the query above but obviously it returns the top 25 row of
SELECT customer
FROM tt
WHERE DocNum LIKE '%something%'
AND U_NAME NOT LIKE '%something%'
GROUP BY customer
ORDER BY MIN(DocNum) DESC
How can I get the
WHERE DocNum LIKE '%something%'
AND U_NAME NOT LIKE '%something%'
GROUP BY customer
ORDER BY MIN(DocNum) DESC
of the top 25 rows of customer?
2条答案
按热度按时间siv3szwd1#
You want the top 25 customers (which are the 25 with the least docnums) first. For these customers you want to select all rows that match certain docnum and u_name patterns:
0aydgbwb2#
The
WHERE
clause is evaluating before theTOP
andORDER BY
. If you want to first get theTOP 25
records order byMIN(DocNum) DESC
then you need to perform this operation first. This can be achieved using sub-query like the following: