Return only the first alphabetical SELECT result

I am trying to return only the first query result after sorting in alphabetical order:

SELECT MIN(DisplayName) FROM [User] GROUP BY DisplayName 

I figured the above would do the trick. But he still saves all the results in alphabetical order. How to get the desired results?

+2
source share
6 answers

You do not need GROUP BY :

 SELECT MIN(DisplayName) FROM [User] 

OR

 SELECT TOP 1 DisplayName FROM [User] ORDER BY DisplayName 

GROUP BY returns each individual value / combination of values โ€‹โ€‹for the listed fields.

+6
source

Just the first line? Just delete the group:

 SELECT MIN(DisplayName) FROM [User] 
+1
source

To return the first record, you can try the following:

 Select TOP 1 MIN(DisplayName) FROM [User] ORDER BY DisplayName 

Will sort in ascending order and it will select the first record

+1
source

Here is another method if you want to accept the โ€œresultโ€ and then join the antoher table.

 Use Northwind GO Select derived1.CustomerID, o.* from dbo.Orders o join ( SELECT top 1 CustomerID FROM [Customers] customers ORder by CustomerID ) as derived1 on derived1.CustomerID = o.CustomerID 
+1
source

I think you want one entry per name to be initialized, if so, try following

here is a working demonstration of SQLFiddle

SELECT Left (DisplayName, 1), MIN (DisplayName) FROM [User] GROUP BY Left (DisplayName, 1)

+1
source
 select top 1 city, Len(city) from station group by Len(city), city order by Len(city) asc select top 1 city, Len(city) from station group by Len(city), city order by Len(city) desc 
0
source

All Articles