如何根据oracle join中的列值只获取一条记录
how to get only one record based on column value in oracle join
加入时我只需要获取列值 1 的一条记录 table1
,谁能告诉我这在 Oracle 中可以实现吗?
目前我有
join (select * from table2 where column1='1') tab2result
on table1.column1 = tab2result.column1.
加入时,我只需要第一条不同 column1
值的记录。
您可以在子查询中使用聚合 max
(或 min
)函数:
SELECT t1.colum1, t1.column2, t2.column1, t2.column2
FROM table1 t1
JOIN (SELECT column1, MAX(column2) AS column2
FROM table2
GROUP BY column1) t2 ON t1.column1 = t2.column1
基本上,
Select t1.column1
, t1.column2
, t2first.column2
From table t1
Join (
Select row_number() over (partition by t2.column1 order by t2.column2) AS rn
, t2.column1
, t2.column2
from table t2
) t2first on ( t2first.column1 = t1.column1 )
where t2first.rn = 1
;
加入时我只需要获取列值 1 的一条记录 table1
,谁能告诉我这在 Oracle 中可以实现吗?
目前我有
join (select * from table2 where column1='1') tab2result
on table1.column1 = tab2result.column1.
加入时,我只需要第一条不同 column1
值的记录。
您可以在子查询中使用聚合 max
(或 min
)函数:
SELECT t1.colum1, t1.column2, t2.column1, t2.column2
FROM table1 t1
JOIN (SELECT column1, MAX(column2) AS column2
FROM table2
GROUP BY column1) t2 ON t1.column1 = t2.column1
基本上,
Select t1.column1
, t1.column2
, t2first.column2
From table t1
Join (
Select row_number() over (partition by t2.column1 order by t2.column2) AS rn
, t2.column1
, t2.column2
from table t2
) t2first on ( t2first.column1 = t1.column1 )
where t2first.rn = 1
;