首页 > 解决方案 > 如何在 SQL 中的一列上选择不重复的记录?

问题描述

我有一个包含 3 列的表格,如下所示:

+------------+---------------+-------+  
| Industry      | country    | price |  
+------------+---------------+-------+  
Retail          |UK              | 20
Retail          | Japan          | 30
Retail          | Germany        | 40
Retail          | America        | 50

我想要的结果是这样的:

+------------+---------------+-------+  
| Industry      |    country  | price |  
+------------+---------------+-------+  
Retail         | UK             | 20
               | Japan          | 40
               | Germany        | 40
               | America        | 50

我需要没有任何重复的行业类别。最好的 SQL 命令是什么?

标签: mysqlsqlselectduplicates

解决方案


如果适合您,您可以尝试以下方法。我也添加了 SQL 小提琴链接

http://sqlfiddle.com/#!4/d162a4/10

select (case when row_id=1 then industry else ' ' end) industry
        ,country
        ,price
from (select industry,country
            ,price
            ,row_number() over(partition by industry order by rownum) row_id
      from prices
     ) tmp

推荐阅读