简体   繁体   English

如何为每个不同的column1值从(非唯一)column2中仅检索一条记录?

[英]how to retrieve just one record from (non-distinct) column2 for every distinct column1 value?

RDBMS - Oracle 11g. RDBMS-Oracle 11g。

Question: I have a table like this - 问题:我有一个这样的桌子-

column1   column2
A         111
A         222
A         333
B         444
C         555
C         666

I want to write a query that picks up just one record for each distinct column1 value. 我想编写一个查询,它为每个不同的column1值仅获取一条记录。 The output should be this way: 输出应该是这样的:

A         111
B         444
C         555

I am not worried about which value in corresponding column2 gets picked up. 我不担心相应的column2中的哪个值会被提取。 Only constraint is I just want a singe record being fetched for each of the distinct column1 value. 唯一的约束是我只想为每个不同的column1值提取一个单一记录。

I cannot think of a way to do this without using procedures. 我无法想到一种无需使用过程即可执行此操作的方法。 Is this possible with just sql queries (no function/ procedure)? 仅使用SQL查询(没有函数/过程)是否可能? Thanks. 谢谢。

Sure 当然

SELECT column1, MIN(column2)
  FROM your_table
 GROUP BY column1

Since you don't care which column2 value you get, you could also use MAX or many other aggregate functions instead of MIN . 由于您不在乎获得哪个column2值,因此也可以使用MAX或许多其他聚合函数来代替MIN

If you don't care, let the column2 be MIN 如果您不在乎,请让column2为MIN

  SELECT column1, 
         MIN(column2) 
    FROM table 
GROUP BY column1;
Select column1,MIN(column2)
From tableName
group by column1

SQL Fiddle Demo SQL小提琴演示

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

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