我如何编写一个查询,该查询为一列中的每个不同值返回一行,并从另一列返回任意值?

时间:2021-05-25 15:29:42

I'd like to find a, hopefully simple, solution to the following problem.

我想找到一个,希望很简单的方法来解决下面的问题。

I have a table like this

我有一张这样的桌子

Name -  GUID 

NameA   {AH42-AJG5-AFHA}      
NameA   {AJD4-AFJ4-HVFA}      
NameB   {BGA4-AJGA-GHAA}
NameB   {JGA8-GGK1-KLP9}      
NameA   {KGA4-JAD4-GJA9}

An example of my desired outcome is

我所期望的结果的一个例子是

NameA   {AH42-AJG5-AFHA} 
NameB   {BGA4-AJGA-GHAA}

I want exactly 1 entry for a specific name, and I need any GUID which was associated with this name in the second column. (The GUID that is returned is arbitrary)

我需要一个特定名称的条目,并且我需要在第二列中与这个名称关联的任何GUID。(返回的GUID是任意的)

Thanks for your advice.

谢谢你的建议。

2 个解决方案

#1


4  

Assuming what GUID is returned is irrelevant; so long as it has an Associate to one of the names.

假设返回的是GUID,这无关紧要;只要它和其中一个名字有关联。

Select [name], min([GUID]) as mGuid
FROM tableLikeThis
Group by [Name]

#2


0  

Just to mention an alternate way of doing it (xQbert already answered the question). You could do something along:

只是提一下另一种方法(xQbert已经回答了这个问题)。你可以做点什么:

SELECT DISTINCT ON (Guid) Name, Guid  
FROM Table  

#1


4  

Assuming what GUID is returned is irrelevant; so long as it has an Associate to one of the names.

假设返回的是GUID,这无关紧要;只要它和其中一个名字有关联。

Select [name], min([GUID]) as mGuid
FROM tableLikeThis
Group by [Name]

#2


0  

Just to mention an alternate way of doing it (xQbert already answered the question). You could do something along:

只是提一下另一种方法(xQbert已经回答了这个问题)。你可以做点什么:

SELECT DISTINCT ON (Guid) Name, Guid  
FROM Table