获得LEFT OUTER JOIN的第一行

时间:2012-05-11 03:37:02

标签: sql oracle left-join

我有3张桌子:

(SELECT DISTINCT ID
FROM IDS)a
LEFT OUTER JOIN
(SELECT NAME, ID
FROM NAMES)b
ON a.ID = b.ID
LEFT OUTER JOIN
(SELECT ADDRESS FROM ADDRESSES
WHERE ROWNUM <2
ORDER BY UPDATED_DATE DESC)c
ON a.ID = c.ID

ID只能有一个名称,但可以有多个地址。我只想要最新的一个。即使存在地址,此查询也会将地址返回为null,因为它只会从表中获取第一个地址,然后尝试LEFT JOIN到它无法找到的地址ID。编写此查询的正确方法是什么?

4 个答案:

答案 0 :(得分:10)

尝试 KEEP DENSE_RANK

数据来源:

CREATE TABLE person
    (person_id int primary key, firstname varchar2(4), lastname varchar2(9))
/
INSERT ALL
    INTO person (person_id, firstname, lastname)
         VALUES (1, 'john', 'lennon')
    INTO person (person_id, firstname, lastname)
         VALUES (2, 'paul', 'mccartney')
SELECT * FROM dual;



CREATE TABLE address
    (person_id int, address_id int primary key, city varchar2(8))
/
INSERT ALL
    INTO address (person_id, address_id, city)
         VALUES (1, 1, 'new york')
    INTO address (person_id, address_id, city)
         VALUES (1, 2, 'england')
    INTO address (person_id, address_id, city)
         VALUES (1, 3, 'japan')
    INTO address (person_id, address_id, city)
         VALUES (2, 4, 'london')
SELECT * FROM dual;

查询:

    select  

      p.person_id, p.firstname, p.lastname,

      x.recent_city

    from person p
    left join (

        select person_id,      

            min(city) -- can change this to max(city). will work regardless of min/max

            -- important you do this to get the recent: keep(dense_rank last)

            keep(dense_rank last order by address_id) 
               as recent_city

        from address 
        group by person_id


    ) x on x.person_id = p.person_id

实时测试:http://www.sqlfiddle.com/#!4/7b1c9/2


并非所有数据库都具有与Oracle的KEEP DENSE_RANK窗口函数类似的功能,您可以使用纯窗口函数代替:

select  

  p.person_id, p.firstname, p.lastname,

  x.recent_city, x.pick_one_only

from person p
left join (

    select 

        person_id,      

        row_number() over(partition by person_id order by address_id desc) as pick_one_only,
        city as recent_city

    from address 



) x on x.person_id = p.person_id and x.pick_one_only = 1

实时测试:http://www.sqlfiddle.com/#!4/7b1c9/48


或者使用元组测试,应该对不支持窗口函数的数据库起作用:

select  

  p.person_id, p.firstname, p.lastname,

  x.recent_city

from person p
left join (

    select   
        person_id,city as recent_city    
    from address 
    where (person_id,address_id) in

          (select person_id, max(address_id)
           from address
           group by person_id)



) x on x.person_id = p.person_id 

实时测试:http://www.sqlfiddle.com/#!4/7b1c9/21


并非所有数据库都支持像前面代码中那样的元组测试。您可以改为使用JOIN:

select  

  p.person_id, p.firstname, p.lastname,

  x.recent_city

from person p
left join (

    select 

        address.person_id,address.city as recent_city

    from address 
    join 
    (
          select person_id, max(address_id) as recent_id
           from address
           group by person_id
    ) r 
    ON address.person_id = r.person_id
    AND address.address_id = r.recent_id



) x on x.person_id = p.person_id 

实时测试:http://www.sqlfiddle.com/#!4/7b1c9/24

答案 1 :(得分:2)

您可以使用分析函数RANK

(SELECT DISTINCT ID
   FROM IDS) a
LEFT OUTER JOIN
(SELECT NAME, ID
   FROM NAMES) b
ON a.ID = b.ID
LEFT OUTER JOIN
(SELECT ADDRESS ,
        rank() over (partition by id
                         order by updated_date desc) rnk
   FROM ADDRESSES) c
ON (    a.ID = c.ID 
    and c.rnk = 1)

答案 2 :(得分:1)

目前无法访问任何数据库,您应该可以

(SELECT DISTINCT ID 
    FROM IDS) a LEFT OUTER JOIN
(SELECT NAME, ID 
    FROM NAMES)b ON a.ID = b.ID LEFT OUTER JOIN
(SELECT TOP 1 ADDRESS 
    FROM ADDRESSES 
    ORDER BY UPDATED_DATE DESC) c ON a.ID = c.ID

正如您所看到的,&#34; TOP 1&#34; at&#39;地址&#39;只返回结果集的第一行。 另外,你确定a.ID和c.ID是一样的吗? 我想你需要类似.... c ON a.ID = c.AddressID 如果没有,我不完全确定如何将多个地址链接到一个ID。

答案 3 :(得分:1)

(SELECT DISTINCT ID
FROM IDS)a
LEFT OUTER JOIN
(SELECT NAME, ID
FROM NAMES)b
ON a.ID = b.ID
LEFT OUTER JOIN
(SELECT ADDRESS, ROWNUMBER() OVER(PARTITON BY ID ORDER BY UPDATED_DATE DESC) RN
 FROM ADDRESSES 
)c
ON a.ID = c.ID
where c.RN=1