当其中两行需要唯一时,选择三行(SQL)

时间:2010-09-14 18:30:15

标签: sql database oracle select

首先我使用的是Oracle 10g Express

所以我想选择三列:

[domain_name] [index_path] [collection_name]

现在有两列我想要独一无二:

[domain_name] [index_path]

所以我的问题是基本上如何:

select unique domain_name, index_path from TABLENAMEHERE

同时还选择列[collection_name]

4 个答案:

答案 0 :(得分:3)

如果domain_name,index_path的某些组合有多行,那么您希望从这些行中看到哪个collection_name值?如果你不介意做哪个值:

SELECT domain_name, index_path, MIN(collection_name) collection_name
FROM TABLENAMEHERE
GROUP BY domain_name, index_path;

答案 1 :(得分:1)

select domain_name, index_path, collection_name
  from my_table outr
       inner join 
         (select domain_name, index_path, collection_name, 
                 max(gen_timestamp) 
                    over (partition by domain_name, index_path) gen_timestamp
            from my_table) innr
 where outr.domain_name = innr.domain_name
   and outr.index_path  = innr.index_path
   and outr.collection_name = innr.collection_name
   and outr.gen_timestamp   = innr.gen_timestamp

答案 2 :(得分:0)

只需在查询中将unique替换为distinct

...喔

那应该是这样的:

select [domain_name], [index_path], min([collection_name]) 
from TABLENAMEHERE
group by [domain_name], [index_path]

答案 3 :(得分:0)

您可以按domain_name和index_path进行分组,并在每个组中包含collection_name的一个(最小或最大)值。