【问题标题】:Not getting unique values inspite of using the distinct function尽管使用了 distinct 函数,但没有获得唯一值
【发布时间】:2021-10-12 17:24:29
【问题描述】:

我正在使用以下代码返回一组不同的 UUID 以及对这些 UUID 执行第一次操作的相应日期。原始数据将具有不明确的 UUID 和相应的执行操作日期。我正在尝试提取唯一的 UUID 和执行该操作的第一个日期,如 date1 所示。有人可以帮忙解决我哪里出错了。

我得到的输出是相同的原始数据,不幸的是 UUID 不是唯一的并且有很多重复

with raw_data as (
select UUID, cast(datestring as timestamp) as date1
from raw)

select
distinct UUID,
date_trunc('week', date1)
from raw_date

【问题讨论】:

    标签: sql presto


    【解决方案1】:

    使用min()聚合函数:

    select UUID,
           min(date_trunc('week', cast(datestring as timestamp))) 
    from raw
    group by UUID;
    

    这应该完成您的查询所做的一切。不需要子查询或 CTE。

    【讨论】:

    • 谢谢。我应该在子查询中这样做吗?还是应该在主查询中?我尝试在主查询中执行主要功能。我使用了代码 distinct uuid, min(date_trunc('week',cast(datestromg as timestamp))) 但输出给了我唯一的 UUID 但我在第二列中只得到一个输出。理想情况下应该至少有 3-4 个时间戳,这是我所期望的
    猜你喜欢
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2015-07-14
    相关资源
    最近更新 更多