我有一个客户表,其中有重复的名字和姓氏。我不知道应该使用什么类型的选择查询来显示有关相同条目的特定行。
示例:
表:customers
customer_id|firstname|lastname
-----------+---------+---------
0000000001 |Peter |Griffin
0000000002 |Peter |Pan
0000000003 |Mary |Magdalene
0000000003 |Mary |Jane
我想要的输出是:
customer_id|firstname|lastname
-----------+---------+---------
0000000001 |Peter |Griffin
0000000003 |Mary |Magdalene
我知道这很简单,但我是 SQL 新手。
答案 0 :(得分:1)
一种方法是使用 DISTINCT ON
:
SELECT DISTINCT ON (firstname)
customer_id, firstname, lastname
FROM customers
ORDER BY firstname, customer_id;
见: