Presto窗口功能 - 与日期有关
我在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_birth | last_4 | c_id | cnt |
---|---|---|---|
1878-09-31 | 1234 | XX-345 | 120 |
1889-02-22 | 4321 | yy-332 | 43 |
1899-13 1899-03-13-13 | 5678 | SS-103 | 11 |
这给出了这样 现在,我试图根据此结果运行窗口函数,并获得这样的东西,并在每个星期或一个月中对其进行排序:
date_周 | 总 | 百分比 |
---|---|---|
1878-09-31 | 100 | 3% |
1878-10-06 | 120 120 | 3.5%3.5% |
1878-10 -11 | 140 | 3.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_birth | last_4 | c_id | cnt |
---|---|---|---|
1878-09-31 | 1234 | xx-345 | 120 |
1889-02-22 | 4321 | yy-332 | 43 |
1899-03-13 | 5678 | ss-123 | 11 |
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_ week | sum | percentage |
---|---|---|
1878-09-31 | 100 | 3% |
1878-10-06 | 120 | 3.5% |
1878-10-11 | 140 | 3.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论