I have the following "COMPANIES_BY_NEWS_REPUTATION" in my JavaDB database (this is some random data to represent the structure)
COMPANY | NEWS_HASH | REPUTATION | DATE
-------------------------------------------------------------------
Company A | 14676757 | 0.12345 | 2011-05-19 15:43:28.0
Company B | 454564556 | 0.78956 | 2011-05-24 18:44:28.0
Company C | 454564556 | 0.78956 | 2011-05-24 18:44:28.0
Company A | -7874564 | 0.12345 | 2011-05-19 15:43:28.0
One news_hash may relate to several companies, while a company may relate to several news. Reputation and date are tied to news_hash.
What I need to do is to calculate the average reputation of the last 5 news for each company. For this, I somehow feel that I need the user to “order” and “shift” in the subquery, as shown in the code below.
select COMPANY, avg(REPUTATION) from
(select * from COMPANY_BY_NEWS_REPUTATION order by "DATE" desc
offset 0 rows fetch next 5 row only) as TR group by COMPANY;
However, JavaDB does not allow either ORDER BY or OFFSET in a subquery. Can someone suggest a working solution for my problem?