views:

107

answers:

1

How could I adapt this query to show the previous, say, 61 weeks?

select 
       to_char(order_date,'IYYY') as iso_year,
       to_char(order_date,'IW') as iso_week,
       sum(sale_amount)
from orders
where 
    to_char(order_date,'IW') <> to_char(SYSDATE) --exclude this week in progress
    and to_char(order_date,'IYYY') = 2010
group by 
         to_char(order_date,'IYYY')
         to_char(order_date,'IW')

My first instinct is to do

where 
    to_char(order_date,'IW') <> to_char(SYSDATE) --exclude this week in progress
    and to_char(order_date,'IYYY') >= to_char(order_date,'IW') - 61

Could I omit the "2010" requirement and limit results to 61 rows? Is there a better way?

Much appreciate any help pointing me in the right direction!

+4  A: 

Can you just do this:

select 
       to_char(order_date,'IYYY') as iso_year,
       to_char(order_date,'IW') as iso_week,
       sum(sale_amount)
from orders
where order_date >= TRUNC(SYSDATE,'IW') - (61 * 7)
  and order_date < TRUNC(SYSDATE,'IW')
group by 
         to_char(order_date,'IYYY'),
         to_char(order_date,'IW')
Jeffrey Kemp
i didn't know about the TRUNC function. Thanks :)