选择三行,其中两行(分组)唯一,另一行由另一列(SQL)过滤
首先,我使用的是Oracle 10g Express 因此,我要选择三列: [域名][索引路径][集合名称] 现在,我希望有两列是唯一的(作为一个组): [域名][索引路径] 然后我想选择另一列[gen_timestamp]最近时显示的行 因此,我的问题是我如何基本上:选择三行,其中两行(分组)唯一,另一行由另一列(SQL)过滤,sql,oracle,plsql,oracle10g,aggregate-functions,Sql,Oracle,Plsql,Oracle10g,Aggregate Functions,首先,我使用的是Oracle 10g Express 因此,我要选择三列: [域名][索引路径][集合名称] 现在,我希望有两列是唯一的(作为一个组): [域名][索引路径] 然后我想选择另一列[gen_timestamp]最近时显示的行 因此,我的问题是我如何基本上: SELECT domain_name, index_path, MIN(collection_name) collection_name FROM TABLENAMEHERE GROUP BY domain_name, inde
SELECT domain_name, index_path, MIN(collection_name) collection_name
FROM TABLENAMEHERE
GROUP BY domain_name, index_path;
但是不要选择min collection_名称,而是选择最近的行[gen_timestamp]
为了澄清一些问题,我可以看到人们问: 您需要域名的唯一值和索引路径的唯一值,还是两者的唯一组合 两者的独特组合。 那么有多行相同的[域名][索引路径] 是。
这是我现在正在使用的代码,但不太管用:
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
如果发生重复的gen_时间戳值,则存在重复的风险:
SELECT x.domain_name,
x.index_path,
x.collection_name
FROM TABLENAMEHERE x
JOIN (SELECT t.domain_name,
t.index_path,
MAX(t.gen_timestamp) AS max_ts
FROM YOUR_TABLE t
GROUP BY t.domain_name, t.index_path) y ON y.domain_name = x.domain_name
AND y.index_path = x.index_path
AND y.max_ts = x.gen_timestamp
ORDER BY domain_name, index_path
使用行号(9i+),无重复风险:
WITH summary AS (
SELECT t.domain_name,
t.index_path,
t.collection_name,
ROW_NUMBER() OVER(PARTITION BY t.domain_name,
t.index_path
ORDER BY t.gen_timestamp DESC) AS rank
FROM YOUR_TABLE t)
SELECT s.domain_name,
s.index_path,
s.collection_name
FROM summary s
WHERE s.rank = 1
ORDER BY domain_name, index_path
如果发生重复的gen_时间戳值,则存在重复的风险:
SELECT x.domain_name,
x.index_path,
x.collection_name
FROM TABLENAMEHERE x
JOIN (SELECT t.domain_name,
t.index_path,
MAX(t.gen_timestamp) AS max_ts
FROM YOUR_TABLE t
GROUP BY t.domain_name, t.index_path) y ON y.domain_name = x.domain_name
AND y.index_path = x.index_path
AND y.max_ts = x.gen_timestamp
ORDER BY domain_name, index_path
使用行号(9i+),无重复风险:
WITH summary AS (
SELECT t.domain_name,
t.index_path,
t.collection_name,
ROW_NUMBER() OVER(PARTITION BY t.domain_name,
t.index_path
ORDER BY t.gen_timestamp DESC) AS rank
FROM YOUR_TABLE t)
SELECT s.domain_name,
s.index_path,
s.collection_name
FROM summary s
WHERE s.rank = 1
ORDER BY domain_name, index_path
自版本9以来,有一个聚合函数可以完全满足您的要求。不幸的是,我还没有在你的两个帖子的回复中看到这一条 演示您的问题的表格:
SQL> create table tablenamehere (domain_name,index_path,collection_name,gen_timestamp)
2 as
3 select 'A', 'Z', 'a collection name', systimestamp from dual union all
4 select 'A', 'Z', 'b collection name', systimestamp - 1 from dual union all
5 select 'A', 'Y', 'c collection name', systimestamp from dual union all
6 select 'B', 'X', 'd collection name', systimestamp - 2 from dual union all
7 select 'B', 'X', 'e collection name', systimestamp - 4 from dual union all
8 select 'B', 'X', 'f collection name', systimestamp from dual
9 /
Table created.
以及显示min(集合名称)的查询。这将显示“d集合名称”,但您希望它显示“f集合名称”:
无需对所有行应用分析函数并对这些结果进行过滤:您正在进行聚合,最后一个函数正好完成了您的工作。以下是文档的链接:
问候,,
Rob。自版本9以来,有一个聚合函数可供使用,它完全满足您的要求。不幸的是,我还没有在你的两个帖子的回复中看到这一条 演示您的问题的表格:
SQL> create table tablenamehere (domain_name,index_path,collection_name,gen_timestamp)
2 as
3 select 'A', 'Z', 'a collection name', systimestamp from dual union all
4 select 'A', 'Z', 'b collection name', systimestamp - 1 from dual union all
5 select 'A', 'Y', 'c collection name', systimestamp from dual union all
6 select 'B', 'X', 'd collection name', systimestamp - 2 from dual union all
7 select 'B', 'X', 'e collection name', systimestamp - 4 from dual union all
8 select 'B', 'X', 'f collection name', systimestamp from dual
9 /
Table created.
以及显示min(集合名称)的查询。这将显示“d集合名称”,但您希望它显示“f集合名称”:
无需对所有行应用分析函数并对这些结果进行过滤:您正在进行聚合,最后一个函数正好完成了您的工作。以下是文档的链接:
问候,,
Rob。选择实际时间戳,同时我要选择时间戳所指的集合名称。类似于我刚才编辑的问题。@jacobnlsn:那么您想要与每个域/路径对的最高
gen\u时间戳相关联的collection\u name
值——正确吗?我想要与每个域/路径对的最高gen\u时间戳相关联的集合名称、域名和索引路径值。所以你非常接近了。这选择了实际的时间戳,而我想选择时间戳所指的集合名称。类似于我刚才编辑的问题。@jacobnlsn:那么您想要与每个域/路径对的最高gen\u时间戳相关联的collection\u name
值——正确吗?我想要与每个域/路径对的最高gen\u时间戳相关联的集合名称、域名和索引路径值。所以你非常接近。非常确定你需要在分析中使用PARTITION BY,或者它只是第一个具有最高时间戳值的集合名称…非常确定你需要在分析中使用PARTITION BY,或者它只是第一个具有最高时间戳值的集合名称。。。