在SQL中按月汇总问题

发布于 2025-02-06 14:17:35 字数 1145 浏览 1 评论 0原文

我想按年,季度,月,一周和我写了一个查询来计算记录以做到这一点,但是发生的事情是我的一周记录在另一个星期的记录中进行,例如,一个月也在发生。 5个记录进入了第6个月。因此,是否有任何方法可以从开始的月底开始修复记录,以便它仅捕获该特定月份的记录,我也希望在一周,季度和年份中使用相同的记录。关于我需要做出的修复/调整可能是什么来纠正这一点?

这是我写的查询。

SELECT

extract(year from date(from_iso8601_timestamp(timestamp_derived))) AS year,
extract(quarter from date(from_iso8601_timestamp(timestamp_derived))) AS quarter,
extract(month from date(from_iso8601_timestamp(timestamp_derived))) AS month,
extract(week from date(from_iso8601_timestamp(timestamp_derived))) AS week,
'MONTHLY' as period,

Region

,COUNT(DISTINCT user_id) as users
 FROM login as l
 INNER JOIN logfiles as f
on l.id = f.user_id
WHERE EXTRACT(year from date(from_iso8601_timestamp(timestamp_derived))) = 2022  
group by 1,2,3,4,5,6
having count(7) >= 1
order by 7 desc

我还为可见性附上了图片。任何建议/帮助将不胜感激。

所需的输出是,它应该根据我的分组捕获每个区域中一行的总用户数量。

这就是我得到的:

在此处输入图像描述

所需的输出:

在此处输入图像描述

I want to count the records by year, quarter, month, week and I have written a query to do that but what's happening is my one week's records are going in another week's records and same is happening with month as well, for instance, month 5 records are going into month 6. So, is there any way to fix the records from month beginning to month end so that it is capturing only records in that particular month, and I want the same for week, quarter and year as well. Any idea on what might be the little fix/tweak that I need to make to correct this?

This is the query I have written.

SELECT

extract(year from date(from_iso8601_timestamp(timestamp_derived))) AS year,
extract(quarter from date(from_iso8601_timestamp(timestamp_derived))) AS quarter,
extract(month from date(from_iso8601_timestamp(timestamp_derived))) AS month,
extract(week from date(from_iso8601_timestamp(timestamp_derived))) AS week,
'MONTHLY' as period,

Region

,COUNT(DISTINCT user_id) as users
 FROM login as l
 INNER JOIN logfiles as f
on l.id = f.user_id
WHERE EXTRACT(year from date(from_iso8601_timestamp(timestamp_derived))) = 2022  
group by 1,2,3,4,5,6
having count(7) >= 1
order by 7 desc

I have also attached a picture for the visibility. Any suggestions/help would be appreciated.

Desired output is it should capture the total users count in one row for each region based on my grouping.

This is what I am getting:

enter image description here

Desired Output:

enter image description here

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

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

发布评论

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