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