两张表,
企业表A,
ID  企业编号,
name  企业名称公告表B
ID  流水号
companyid 企业ID
date  发布时间
现在要求取出最后发布的公告,但是如果有企业发布多条的话,那么只取其中一条。
一共需要取出10条数据。请教一下sql应该怎么写啊。谢谢了

解决方案 »

  1.   

    select * 
    from 
    (
    select a.id, a.name, max(b.date)
    frmo a, b
    where a.id = b.companyid
    group by a.id,a.name
    )
    where rownum <= 10
      

  2.   

    SELECT a.*,c.*
      FROM (SELECT *
              FROM (SELECT b.*, row_number() over(PARTITION BY companyid ORDER BY pdate DESC) rn FROM b),
             WHERE rn = 1
             ORDER BY b.pdate) c,a
     WHERE a.id = b.companyid AND
           rownum < 11;
      

  3.   

    b.companyid  应为 c.companyid 
      

  4.   


    select * from 
    (
    select * from tb a
    where not exists(select 1 from tb b where a.companyid=b.companyid and a.date<b.date)
    order by date
    )
    where rownum<=10
      

  5.   

    select name,id,date
    from(
    select a.name,a.id,b.date,row_number()over(partition by id order by date desc)row_num
    from a,b
    where a.id=b.compantid
    )t where row_num <2 and rownum <11;
      

  6.   

    --分析函数跟伪列
    select 企业编号,企业名称,流水号,最新发布时间
    from (select b.companyid 企业编号,a.name 企业名称,b.id 流水号,b.date 最新发布时间
    row_number() over(partition by b.companyid order by date desc) rn
    from a,b
    where a.id=b.companyid) a
    where rn=1 and rownum<=10
      

  7.   

    select 企业编号,企业名称,流水号,最新发布时间
    from (select b.companyid 企业编号,a.name 企业名称,b.id 流水号,b.date 最新发布时间
    row_number() over(partition by b.companyid order by date desc) rn
    from a,b
    where a.id=b.companyid) t --a
    where rn=1 and rownum<=10
      

  8.   

    饿,谢谢大家了。最后决定采用糖人和duqiangcise的方法。row_number()over(partition by id order by date desc)  这种写法还没用过,一会去研究一下。不过not exists 在我目前的情况下肯定会影响速度的。OVER