[英]I want to group by my data with date and time In postgres SQL and want one row of each groupby column
I want to fetch data from history table and I want to group by usergroupid
and I want only one row of usergroupid 237935 and 761793 out of multiple rows.我想从历史表中获取数据,我想按
usergroupid
分组,我只想要多行中的一行 usergroupid 237935 和 761793。
I get this error我收到这个错误
Column "history.entrytime" must appear in the GROUP BY clause or be used in an aggregate function
“history.entrytime”列必须出现在 GROUP BY 子句中或用于聚合 function
This is my query:这是我的查询:
SELECT
TO_CHAR(entrytime, 'YYYY-MM-DD HH12:MI'), usergroupid
FROM
"public"."history"
WHERE
deviceid = 17355763 usergroupid IN (237935,761793)
GROUP BY
TO_CHAR(entrytime, 'YYYY-MM-DD HH12:MI'), usergroupid
ORDER BY
entrytime DESC
LIMIT 1
I want following output:我想关注 output:
to_char ![]() |
usergroupid![]() |
---|---|
2022-11-19 02:04:10 ![]() |
237935 ![]() |
2022-11-19 02:05:40 ![]() |
761793 ![]() |
This is my demo table:这是我的演示表:
It seems you simply want the maximum entrytime per usergroupid.看来您只是想要每个用户组 ID 的最大进入时间。 In order to get one row per usergroupid, you
GROUP BY usergroupid
.为了每个 usergroupid 获得一行,您
GROUP BY usergroupid
。 In order to get the maximum entrytime, you select MAX(entrytime)
.为了获得最大的进入时间,你 select
MAX(entrytime)
。
SELECT MAX(entrytime), usergroupid
FROM public.history
WHERE deviceid = 17355763 AND usergroupid IN (237935,761793)
GROUP BY usergroupid
ORDER BY usergroupid;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.