最后一个值与最大值(日期)

发布于 2025-01-14 19:04:35 字数 324 浏览 2 评论 0原文

我想找出使用​​ 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来分手还会想你 2025-01-21 19:04:35

有趣..

我用一些虚拟数据创建了一个 CTE,以查看代码是否生成错误等:

WITH USER_DEVICES AS (
    SELECT * FROM VALUES
    ('Apple', '2022-01-01'::date),
    ('Apple', '2022-01-02'::date),
    ('Andriod', '2022-02-01'::date),
    ('Andriod', '2022-02-02'::date)
    t(DEVICE_TYPE, CREATED_AT)
)
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;

它运行良好,并给出:

DEVICEDATE(MAX(CREATED_AT))
Apple2022-01-01
Apple2022-01-02
Andriod2022-02-01
Andriod2022-02-02

所以 date(max(CREATED_AT)) 正在查找最后一个 create_at,然后再次将其转换为日期。其中最后一位是不需要的,因此它可能只是 max(CREATED_AT)

但您的 last_value 是按对这些分区进行排序的相同值进行分区的,因此您只会得到“所有结果”

因此,考虑到所有这些,让我们重新阅读您的问题,并尝试找到真正的意图并猜测代码......

我想了解在 ios 上使用该应用程序以及在 Android 上使用该应用程序的用户总数

这让我觉得您想要每天进行分组,并进行一些计数..

WITH USER_DEVICES AS (
    SELECT * FROM VALUES
    ('Apple', '2022-01-01'::date),
    ('Apple', '2022-01-01'::date),
    ('Andriod', '2022-02-01'::date),
    ('Andriod', '2022-02-01'::date)
    t(DEVICE_TYPE, CREATED_AT)
)
SELECT
    CREATED_AT,
    count_if(DEVICE_TYPE = 'Apple') as apple_count,
    count_if(DEVICE_TYPE = 'Andriod') as andriod_count
FROM USER_DEVICES
GROUP BY CREATED_AT;

这给出了:

CREATED_ATAPPLE_COUNTANDRIOD_COUNT
2022-01-0120
2022-02-0102

但我对所有时间都不感兴趣,我只想知道最后一个值

所以我假设你的意思是你只想要最近一天的结果。

这可以通过 QUALIFY

SELECT
    CREATED_AT,
    count_if(DEVICE_TYPE = 'Apple') as apple_count,
    count_if(DEVICE_TYPE = 'Andriod') as andriod_count
FROM USER_DEVICES
GROUP BY CREATED_AT
QUALIFY ROW_NUMBER() OVER (ORDER BY CREATED_AT DESC) = 1;
CREATED_ATAPPLE_COUNTANDRIOD_COUNT
2022-02-0102来完成

Interesting..

I created a CTE with some dummy data to see if the code generates errors etc, etc:

WITH USER_DEVICES AS (
    SELECT * FROM VALUES
    ('Apple', '2022-01-01'::date),
    ('Apple', '2022-01-02'::date),
    ('Andriod', '2022-02-01'::date),
    ('Andriod', '2022-02-02'::date)
    t(DEVICE_TYPE, CREATED_AT)
)
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;

and it run fine, and gives:

DEVICEDATE(MAX(CREATED_AT))
Apple2022-01-01
Apple2022-01-02
Andriod2022-02-01
Andriod2022-02-02

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 just max(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...

I wanna find out the total number of my app users who are using the app from ios and who are using android

makes me think you want to group per day, and do some counting..

WITH USER_DEVICES AS (
    SELECT * FROM VALUES
    ('Apple', '2022-01-01'::date),
    ('Apple', '2022-01-01'::date),
    ('Andriod', '2022-02-01'::date),
    ('Andriod', '2022-02-01'::date)
    t(DEVICE_TYPE, CREATED_AT)
)
SELECT
    CREATED_AT,
    count_if(DEVICE_TYPE = 'Apple') as apple_count,
    count_if(DEVICE_TYPE = 'Andriod') as andriod_count
FROM USER_DEVICES
GROUP BY CREATED_AT;

which gives:

CREATED_ATAPPLE_COUNTANDRIOD_COUNT
2022-01-0120
2022-02-0102

but I'm not interested in all time I just want to know the last value

so I assume you mean you want just the result for the most resent day.

Which can be done via a QUALIFY

SELECT
    CREATED_AT,
    count_if(DEVICE_TYPE = 'Apple') as apple_count,
    count_if(DEVICE_TYPE = 'Andriod') as andriod_count
FROM USER_DEVICES
GROUP BY CREATED_AT
QUALIFY ROW_NUMBER() OVER (ORDER BY CREATED_AT DESC) = 1;
CREATED_ATAPPLE_COUNTANDRIOD_COUNT
2022-02-0102
一影成城 2025-01-21 19:04:35

如果我理解正确的话,对于每个用户,您希望在聚合每个设备类型的用户之前包含与该用户最近的应用程序活动日期关联的设备类型。

with cte as 
(select *, row_number() over(partition by user_id order by created_at desc) as rn
 from user_devices)
 
 
select device_type, count(*) as counts
from cte
where rn=1
group by device_type;

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.

with cte as 
(select *, row_number() over(partition by user_id order by created_at desc) as rn
 from user_devices)
 
 
select device_type, count(*) as counts
from cte
where rn=1
group by device_type;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文