ppetyr ppetyr - 1 year ago 56
SQL Question

Sql dropdown query

I have the following code:

SELECT DISTINCT [id] As Value, [ServerName] As Text FROM [dbo].[DominoDB]
that is executed on dapper in C# project, to populate a dropdown. I want to have in the dropdown only distinct ServerNames, but in the database table DominoDB i have rows like these: id = 1 ServerName = dominoDB and id = 2 ServerName = dominoDB and as a result in the dropdown i have duplicated names of servers. Can anyone help me to have only distinct server names?

Answer Source
SELECT DISTINCT [ServerName] As Text FROM [dbo].[DominoDB]

And if You need also Value, You can't use distinct for both fields. You must decide if value must be the greatest ID or the smallest one. Then Group by Server name and use MAX/MIN on ID field.

If You choose that option with MIN id:

SELECT MIN(ID) as VALUE, [ServerName] As Text FROM [dbo].[DominoDB] GROUP BY ServerName