简体   繁体   English

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

[英]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.

相关问题 如何为每个不同的column1值从(非唯一)column2中仅检索一条记录? - how to retrieve just one record from (non-distinct) column2 for every distinct column1 value? 查询返回每列中每个值的不同计数 - query that returns distinct count of each value in each column 如何从具有相同值的两个不同列中选择一行 - How select one row from two distinct column with same value 对于SQL一列中的相同ID,如何为另一列中值较大的行编写Case语句? - For the same ID in one column of SQL, how do I write a Case statement for the row with the greater value in the another column? 版本2:如何编写postgres sql查询,返回M +某个列的不同值,返回M +未知的总记录? - Version 2: How can I write a postgres sql query that returns M distinct values of a certain column with M + unknown overall records returned? SQL 从一列返回值的查询是重复的,但在另一列中有 1 null 和一个值 - SQL query that returns the value from one column which is duplicated but in other column has 1 null and one value 如何合并此查询以使其仅返回一列? - How can I merge this query so that it returns only one column? MySQL查询返回的行比预期的多,我如何仅从每个组返回一列的最大值? - MySQL query returns more rows than intended, how do i only return the highest value of one column from each group? 查询还会返回值为0的列 - Query also returns column with value 0 我怎样才能在列中列出一个不同的值? -SQL - How can I only list one distinct value in a column? -SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM