Presto窗口功能 - 与日期有关

发布于 2025-02-04 09:19:15 字数 1628 浏览 2 评论 0原文

我在Presto中有以下代码:

select distinct date_of_birth, substr(trim(tax_id), -4) as last_4, c_id ,  count(*) as cnt 
from schema.table
where tax_id not in ('', ' ')
and length (cast(date_of_birth as varchar)) > 0
group by c_id, date_of_birth , substr(trim(tax_id),-4)
order by cnt desc

的结果:

date_of_birthlast_4c_idcnt
1878-09-311234XX-345120
1889-02-224321yy-33243
1899-13 1899-03-13-135678SS-10311

这给出了这样 现在,我试图根据此结果运行窗口函数,并获得这样的东西,并在每个星期或一个月中对其进行排序:

date_周百分比
1878-09-311003%
1878-10-06120 1203.5%3.5%
1878-10 -111403.6%的

百分比相对于上表中所有CNT的总和。 总和是每7天的总计。

到目前为止,这是我的代码,但是它没有起作用,您能告诉我这里有什么问题吗?

 with cte as (
    select distinct date_of_birth as dob, substr(trim(tax_id), -4) as last_4, count(*) as cnt 
    from schema.table
    where tax_id not in ('', ' ')
    and length (cast(date_of_birth as varchar)) > 0
    group by date_of_birth , substr(trim(tax_id),-4)),
select date_trunc('week', yr) as yr, sum(cnt) over (group by dob rows between 5 preceeding and current row),
from cte 
order by yr;

它无法正常工作,所以我无法找到百分比。

I have the following code in presto:

select distinct date_of_birth, substr(trim(tax_id), -4) as last_4, c_id ,  count(*) as cnt 
from schema.table
where tax_id not in ('', ' ')
and length (cast(date_of_birth as varchar)) > 0
group by c_id, date_of_birth , substr(trim(tax_id),-4)
order by cnt desc

this gives a result like this:

date_of_birthlast_4c_idcnt
1878-09-311234xx-345120
1889-02-224321yy-33243
1899-03-135678ss-12311

however Now I am trying to run a window function based on this result and get something like this and sort it on each week or month:

date_ weeksumpercentage
1878-09-311003%
1878-10-061203.5%
1878-10-111403.6%

percentage is relative to sum of all cnt in the previous table.
sum is the total of the week, for each 7 days.

here is my code so far, but it is not working, could you tell me what is wrong here?

 with cte as (
    select distinct date_of_birth as dob, substr(trim(tax_id), -4) as last_4, count(*) as cnt 
    from schema.table
    where tax_id not in ('', ' ')
    and length (cast(date_of_birth as varchar)) > 0
    group by date_of_birth , substr(trim(tax_id),-4)),
select date_trunc('week', yr) as yr, sum(cnt) over (group by dob rows between 5 preceeding and current row),
from cte 
order by yr;

It was not working so I could not move to finding percentage.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文