如何在sql中找到没有聚合函数的最小值?

w41d8nur  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(373)

我在postgres上有一个示例数据集

updated_at     activated_at        name     gender   role    school  app_name    device_type
-------------+-----------------+----------+--------+-------+---------+---------+---------------
August 2         July 30            Ron        M       S        A         Y          android
August 1         July 30            Ron        M       S        A         Z          browser
July 30          July 30            Ron        M       S        A         Y          android
August 1         July 28            Ana        F       S        B         Y          android
August 1         July 28            Ana        F       S        B         Z          browser
July 28          July 28            Ana        F       S        B         Y          android

我想知道什么时候是用户第一次显示(更新)后,它的激活时间和属于哪个应用程序。
预期结果:

updated_at     activated_at        name     gender   role    school  app_name    device_type
-------------+-----------------+----------+--------+-------+---------+---------+---------------
July 30          July 30            Ron        M       S        A         Y          android
July 28          July 28            Ana        F       S        B         Y          android

我尝试了以下sql:

SELECT min(ut.updated_at), u.activated_at, u.full_name, u.gender, r.name, s.name, ut.app_name, ut.device_type
FROM "public"."user_tokens" ut JOIN
     "public"."users" u
     ON ut.user_id = u.id JOIN
     "public"."user_roles" ur
     ON ut.user_id = ur.user_id JOIN
     "public"."roles" r
     ON ur.role_id = r.id JOIN
     "public"."schools" s
      ON ur.school_id = s.id
WHERE (NOT (ut.app_name) like 'G')
Group by u.activated_at, u.full_name, u.gender, r.name, s.name, ut.app_name, ut.device_type
Order by u.activated_at desc

但结果是这样的:

updated_at     activated_at        name     gender   role    school  app_name    device_type
-------------+-----------------+----------+--------+-------+---------+---------+---------------
August 1         July 30            Ron        M       S        A         Z          browser
July 30          July 30            Ron        M       S        A         Y          android
August 1         July 28            Ana        F       S        B         Z          browser
July 28          July 28            Ana        F       S        B         Y          android

我试图排除 app_name 以及 device_type 从GROUPBY子句 ERROR: column "ut.app_name" must appear in the GROUP BY clause or be used in an aggregate function 你知道怎么解决吗?任何意见都将不胜感激。谢谢您。

a0x5cqrl

a0x5cqrl1#

我想 DISTINCT ON 这可能是最好的办法:

SELECT DISTINCT ON (u.full_name)
    ut.updated_at,
    u.activated_at,
    u.full_name,
    u.gender,
    r.name,
    s.name,
    ut.app_name,
    ut.device_type
FROM "public"."user_tokens" ut
INNER JOIN "public"."users" u ON ut.user_id = u.id
INNER JOIN "public"."user_roles" ur ON ut.user_id = ur.user_id
INNER JOIN "public"."roles" r ON ur.role_id = r.id
INNER JOIN "public"."schools" s ON ur.school_id = s.id
WHERE NOT ut.app_name LIKE 'G'
ORDER BY
    u.full_name,
    ut.updated_at;

上面将为每个全名用户返回一条记录,与前面的记录相对应 updated_at 时间。

eyh26e7m

eyh26e7m2#

您可以先按升序(从旧到新)排序,然后使用limit来指定要显示的记录数

select * from TABLE_NAME order by updated_at asc LIMIT 2;

相关问题