最后一个值与最大值(日期)
我想找出使用 ios 应用程序和使用 android 应用程序的应用程序用户总数,但我对所有时间都不感兴趣,我只想知道最后一个值。 所以,我知道我将使用 lastvalue 和 max(date) 但在运行代码时遇到一些错误。 我将在这里分享代码:
SELECT last_value(DEVICE_TYPE) OVER (PARTITION BY CREATED_AT ORDER BY CREATED_AT) AS Device, date(max(CREATED_AT)) FROM USER_DEVICES
GROUP BY DEVICE_TYPE, CREATED_AT
I wanna find out the total number of my app users who are using the app from ios and who are using android, but I'm not interested in all time I just want to know the last value.
So, I know that I'll be using lastvalue and max(date) but I'm facing some errors while running the code.
I'll be sharing the code here:
SELECT last_value(DEVICE_TYPE) OVER (PARTITION BY CREATED_AT ORDER BY CREATED_AT) AS Device, date(max(CREATED_AT)) FROM USER_DEVICES
GROUP BY DEVICE_TYPE, CREATED_AT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣..
我用一些虚拟数据创建了一个 CTE,以查看代码是否生成错误等:
它运行良好,并给出:
所以
date(max(CREATED_AT))
正在查找最后一个 create_at,然后再次将其转换为日期。其中最后一位是不需要的,因此它可能只是max(CREATED_AT)
但您的
last_value
是按对这些分区进行排序的相同值进行分区的,因此您只会得到“所有结果”因此,考虑到所有这些,让我们重新阅读您的问题,并尝试找到真正的意图并猜测代码......
这让我觉得您想要每天进行分组,并进行一些计数..
这给出了:
所以我假设你的意思是你只想要最近一天的结果。
这可以通过 QUALIFY
Interesting..
I created a CTE with some dummy data to see if the code generates errors etc, etc:
and it run fine, and gives:
So the
date(max(CREATED_AT))
is finding the last create_at and then casting that to a date again. Which the last bit is not needed, so it could be justmax(CREATED_AT)
But your
last_value
is partition'ed by the same value that is ordering those partitions, so you will just get "all results"So given all that, lets re-read your question, and try find the true intent and guess the code...
makes me think you want to group per day, and do some counting..
which gives:
so I assume you mean you want just the result for the most resent day.
Which can be done via a QUALIFY
如果我理解正确的话,对于每个用户,您希望在聚合每个设备类型的用户之前包含与该用户最近的应用程序活动日期关联的设备类型。
If I understood you right-- for each user, you want to include the device type associated with the date of most recent app activity for that user before you aggregate users per device type.