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?
You do not need GROUP BY :
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.
Just the first line? Just delete the group:
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
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
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)
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