SQL Server valid way to use TOP, ORDER BY and DISTINCT in the same query

at0kjp5o  于 2023-03-07  发布在  其他
关注(0)|答案(1)|浏览(119)

What is a valid way to write this query?

I need to order the column customer by DocNum descending and get the top 25 without any duplicate.

SELECT DISTINCT TOP 25 customer
FROM tt
WHERE DocNum LIKE '%_%' AND UPPER(U_NAME) NOT LIKE '%E G%'
ORDER BY DocNum DESC
5f0d552i

5f0d552i1#

SELECT TOP 25 customer 
FROM tt
WHERE DocNum LIKE '%_%' AND 
U_NAME NOT LIKE '%E G%' 
GROUP BY customer
ORDER BY MAX(DocNum) DESC

works thanks to Martin Smith

相关问题