查询最新3周的雪花数据查询

发布于 2025-02-13 20:55:16 字数 2504 浏览 0 评论 0原文

在雪花查询中,我想根据具有与数据类型为数字(不是时间戳记)的WorkWeek编号的列中查询最新3周的数据。

数据库看起来像这样:

WorkweekData1Data2
202235......
202235......
202235......
202234......
202233......
202233...我的 。

......
202233......
202233......

到目前为止我正在使用的代码是一种捕获最新3周数据的非常手动的方法,我希望新代码能够捕获最新的3周数据随着新的一周进入DB:

Select *
From DB
where WorkWeek in ('202235','202234','202233')

In a Snowflake query, I will like to query for the latest 3 weeks of data based on a column that has the WorkWeek number with the data type as Number (not timestamp).

My Database looks like this:

WorkWeekData1Data2
202235......
202235......
202235......
202234......
202233......
202233......
202232......
202232......

What I want (latest 3 Workweeks data):

WorkWeekData1Data2
202235......
202235......
202235......
202234......
202233......
202233......

What I am using so far the code is a pretty manual way of catching the latest 3 weeks data, I will like the new code to be able to catch the latest 3 weeks data as a new week rolls into the db:

Select *
From DB
where WorkWeek in ('202235','202234','202233')

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

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

发布评论

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

评论(1

吖咩 2025-02-20 20:55:16

您需要的是浓密的等级:

select column1 as WorkWeek, column2 as SomeData from values
   (202235,'a')
    ,(202235,'b')
    ,(202235,'c')
    ,(202234,'d')
    ,(202233,'e')
    ,(202233,'f')
    ,(202232,'g')
    ,(202232,'h')
qualify dense_rank() over (order by WorkWeek desc) <= 3;

Qualify with Dense Rank is what you need:

select column1 as WorkWeek, column2 as SomeData from values
   (202235,'a')
    ,(202235,'b')
    ,(202235,'c')
    ,(202234,'d')
    ,(202233,'e')
    ,(202233,'f')
    ,(202232,'g')
    ,(202232,'h')
qualify dense_rank() over (order by WorkWeek desc) <= 3;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文