简体   繁体   English

我怎样才能在列中列出一个不同的值? -SQL

[英]How can I only list one distinct value in a column? -SQL

I've got several columns of data. 我有几列数据。 The first column has all unique values that I only want to show up once. 第一列包含我只想显示一次的所有唯一值。 The second column may have multiple entries for the same data. 第二列可以具有用于相同数据的多个条目。 This is causing the first column to show multiple entries, one for each entry in the second column. 这导致第一列显示多个条目,第二列中的每个条目都有一个条目。

Example: 例:

A   123
A   432
A   2352
B   5342
C   34256
C   23423

I only want to see one row for A, one row for B, and one row for C. I don't care which value from the second column is displayed for each A/B/C row. 我只想看A的一行,B的一行,C的一行。我不关心每个A / B / C行显示第二列的哪一个值。

Use GROUP BY clause. 使用GROUP BY子句。

The GROUP BY clause can be used in an SQL SELECT statement to collect data across multiple records and group the results by one or more columns. GROUP BY子句可以在SQL SELECT语句中用于跨多个记录收集数据,并按一列或多列对结果进行分组。

SELECT  col1, MAX(col2) col2
FROM    tableName
GROUP   BY col1

You can use an aggregate function to get the max or min value of the second column and then apply a group by to col1 : 您可以使用聚合函数来获取第二列的maxmin ,然后将group by应用于col1

select col1, max(col2) as col2
from yourtable
group by col1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM