访问带聚合函数的裸列 w/o 添加到分组依据
Access Bare Columns w/ Aggregate Function w/o adding to Group By
我在 postgres 中有 2 个 table。
用户
auth0_id
email
123-A
a@a
123-B
b@b
123-C
c@c
auth0_logs
id
date
user_id
client_name
abc-1
021-10-16T00:18:41.381Z
123-A
example_client
abc-2
...
123-A
example_client
abc-3
...
123-B
example_client
abc-4
...
123-A
example_client
abc-5
...
123-B
example_client
abc-6
...
123-C
example_client
我正在尝试为每个唯一用户 (auth0_logs.user_id) 加入到用户 table on user.auth0_id.
[
{
// auth0_logs information
user_id: "123-A",
last_login: "021-10-16T00:18:41.381Z",
client_name: "example_client",
// users information
email: "a@a"
},
{
user_id: "123-B",
last_login: "...",
client_name: "example_client",
email: "b@b"
},
{
user_id: "123-C",
last_login: "...",
client_name: "example_client",
email: "c@c"
}
]
我知道这是一个问题,在使用聚合器的查询中不允许使用“裸”列(没有添加到 GROUP BY —— 但添加到 GROUP BY 返回 > 1 行)但我无法获得适用于其他 SO post 的解决方案(我发现的最佳 post:SQL select only rows with max value on a column)。我向你保证,在过去的几天里,我已经为此花费了很多时间....
-- 编辑:开始--
我已经删除了我不正确的尝试,以免混淆/误导未来的读者。请根据上述信息使用 WITH
子句查看@MichaelRobellard 的回答。
-- 编辑:结束--
任何帮助或进一步的研究方向将不胜感激!
with
t as
(
select distinct on (user_id) *
from login_logs
order by user_id, ldate desc
),
tt as
(
select auth0_id user_id, ldate last_login, client_name, email
from t join users on auth0_id = user_id
)
select json_agg(to_json(tt.*)) from tt;
SQL fiddle here.
with user_data as (
select user_id, max(date) from auth0_logs group by user_id
)
select * from user_data
join auth0_logs on user_data.user_id = auth0_logs.user_id
and user_data.date = auth0_logs.date
join users on user_data.user_id = users.auth0_id
我在 postgres 中有 2 个 table。
用户
auth0_id | |
---|---|
123-A | a@a |
123-B | b@b |
123-C | c@c |
auth0_logs
id | date | user_id | client_name |
---|---|---|---|
abc-1 | 021-10-16T00:18:41.381Z | 123-A | example_client |
abc-2 | ... | 123-A | example_client |
abc-3 | ... | 123-B | example_client |
abc-4 | ... | 123-A | example_client |
abc-5 | ... | 123-B | example_client |
abc-6 | ... | 123-C | example_client |
我正在尝试为每个唯一用户 (auth0_logs.user_id) 加入到用户 table on user.auth0_id.
[
{
// auth0_logs information
user_id: "123-A",
last_login: "021-10-16T00:18:41.381Z",
client_name: "example_client",
// users information
email: "a@a"
},
{
user_id: "123-B",
last_login: "...",
client_name: "example_client",
email: "b@b"
},
{
user_id: "123-C",
last_login: "...",
client_name: "example_client",
email: "c@c"
}
]
我知道这是一个问题,在使用聚合器的查询中不允许使用“裸”列(没有添加到 GROUP BY —— 但添加到 GROUP BY 返回 > 1 行)但我无法获得适用于其他 SO post 的解决方案(我发现的最佳 post:SQL select only rows with max value on a column)。我向你保证,在过去的几天里,我已经为此花费了很多时间....
-- 编辑:开始--
我已经删除了我不正确的尝试,以免混淆/误导未来的读者。请根据上述信息使用 WITH
子句查看@MichaelRobellard 的回答。
-- 编辑:结束--
任何帮助或进一步的研究方向将不胜感激!
with
t as
(
select distinct on (user_id) *
from login_logs
order by user_id, ldate desc
),
tt as
(
select auth0_id user_id, ldate last_login, client_name, email
from t join users on auth0_id = user_id
)
select json_agg(to_json(tt.*)) from tt;
SQL fiddle here.
with user_data as (
select user_id, max(date) from auth0_logs group by user_id
)
select * from user_data
join auth0_logs on user_data.user_id = auth0_logs.user_id
and user_data.date = auth0_logs.date
join users on user_data.user_id = users.auth0_id