左连接表连接重复值
PEOPLE 表; 人员表 id;
TRANSFER 表 门进记录表 p_id;
id 与p_id 对应;
select pp.name,trn.time,trn.type from people pp ,
transfer trn
where pp.id=trn.p_id(+);
如何查出来 每个人的最近的 一次进出记录?
张三, 十点,进门
李四, 十二点,出门
上面的 语句查出啦,用重复的人员记录;
张三,十点,进门
李四, 十二点,出门
张三,九点,出门;
求指点;
------解决方案--------------------with PEOPLE AS (
select '001' AS id ,'A' AS p_name FROM DUAL
UNION ALL
select '002' AS id ,'B' AS p_name FROM DUAL
),
TRANSFER AS (
select '001' AS p_id ,'9' AS P_time,'IN' AS P_type FROM DUAL
UNION ALL
select '002' AS p_id ,'10' AS P_time,'OUT' AS P_type FROM DUAL
UNION ALL
select '001' AS p_id ,'11' AS P_time,'OUT' AS P_type FROM DUAL
UNION ALL
select '002' AS p_id ,'8' AS P_time,'IN' AS P_type FROM DUAL
UNION ALL
select '002' AS p_id ,'7' AS P_time,'IN' AS P_type FROM DUAL
)
select p_name, P_time, P_type
from (SELECT ROW_NUMBER() OVER(PARTITION BY P.id ORDER BY T.P_time) as rn,
p_name,
P_time,
P_type
FROM PEOPLE P
LEFT JOIN TRANSFER T ON P.id = T.p_id)
where rn = 1