[英]How can I write a query that returns a row for every distinct value in one column and returns an arbitrary value from another column?
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. 我想要一个特定名称恰好1个条目,并且我需要第二列中与此名称相关联的任何GUID。 (The GUID that is returned is arbitrary)
(返回的GUID是任意的)
Thanks for your advice. 谢谢你的建议。
Assuming what GUID is returned is irrelevant; 假设返回的GUID是无关紧要的; so long as it has an Associate to one of the names.
只要它具有与其中一个名称关联的名称。
Select [name], min([GUID]) as mGuid
FROM tableLikeThis
Group by [Name]
Just to mention an alternate way of doing it (xQbert already answered the question). 仅提及一种替代方法(xQbert已经回答了问题)。 You could do something along:
您可以采取以下措施:
SELECT DISTINCT ON (Guid) Name, Guid
FROM Table
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.