在SQL中按月汇总问题
我想按年,季度,月,一周和我写了一个查询来计算记录以做到这一点,但是发生的事情是我的一周记录在另一个星期的记录中进行,例如,一个月也在发生。 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:
Desired Output:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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