开发者

Redundancy in doing sum()

table1 -> id, time_stamp, value

This table consists of 10 id's. Each id would be having a value for each hour in a day. So for 1 day, there would be 240 records in this table.

table2 -> id

Table2 consists of a dynamically changing subset of id's present in table1.

At a particular instance, the intention is to get sum(value) from table1, considering id's only in table2, grouping by each hour in that day, giving the summarized values a rank and repeating this each da开发者_如何学编程y.

the query is at this stage:

select time_stamp, sum(value),
rank() over (partition by trunc(time_stamp) order by sum(value) desc) rn
from table1
where exists (select t2.id from table2 t2 where id=t2.id)
and
time_stamp >= to_date('05/04/2010 00','dd/mm/yyyy hh24') and
time_stamp <= to_date('25/04/2010 23','dd/mm/yyyy hh24')
group by time_stamp
order by time_stamp asc

If the query is correct, can this be made more efficient, considering that, table1 will actually consist of thousand's of id's instead of 10 ?

EDIT: I am using sum(value) 2 times in the query, which I am not able to get a workaround such that the sum() is done only once. Pls help on this


from table1 where exists (select t2.id from table2 t2 where value=t2.value)

The table2 doesn't have Value field. Why is the above query with t2.Value?
You could use a join here

from table1 t1 join table2 t2 on  t1.id = t2.id

EDIT: Its been a while that I worked on Oracle. Pardon me, if my comment on t2.Value doesn't make sense.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜