2

我如何调整这个查询来显示之前的,比如 61 周?

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')

我的第一直觉是做

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

我可以省略“2010”要求并将结果限制为 61 行吗?有没有更好的办法?

非常感谢任何为我指明正确方向的帮助!

4

1 回答 1

4

你能不能这样做:

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')
于 2010-03-24T07:40:54.010 回答