Oracle SQL:前 X 个等周 () 的查询结果(其中 X 可能 > 52)

发布于 2024-08-26 04:31:27 字数 687 浏览 6 评论 0原文

我如何调整此查询以显示之前(例如 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 行吗?有更好的办法吗?

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽手叙旧 2024-09-02 04:31:27

你可以这样做吗:

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

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文