如何从 postgresql 中的案例名称 select
how to select from case name in postgresql
我使用 postgres v14,
我创建了一个包含 5 个时间列的 postgres 视图。
每个用户 ID 在第一列都有一个日期
表示每个userid超过4行
我想获得最大的一排
我的列是 (id,created_at,newstage_created_at,newstage_updated_at,newdata_created_at)
看起来像
id
created_at
newstage_created_at
newstage_updated_at
newdata_created_at
1
2020-11-23 11:28:05
2020-11-23 11:28:05
2020-11-23 11:28:05
null
1
2020-11-23 11:28:05
2020-11-23 11:28:05
2020-11-24 08:13:48
2020-11-25 08:13:48
1
2020-11-23 11:28:05
2020-10-11 05:55:36
2021-01-11 05:55:36
2020-10-11 05:55:36
2
2022-02-09 18:41:42
2022-02-09 18:41:42
2022-02-09 18:41:42
2022-02-09 18:41:42
2
2022-02-09 18:41:42
2022-02-09 18:41:42
2022-02-09 18:41:42
null
2
2022-02-09 18:41:42
2020-10-12 09:55:31
2022-02-09 18:41:42
2022-02-09 18:41:42
2
2022-02-09 18:41:42
2020-10-12 09:55:31
2022-02-09 18:41:42
2022-02-09 18:41:42
这里我需要得到具有最大列和最大行的行
我做了这个sql
case greatest(created_at, newstage_created_at ,newstage_updated_at,newdata_created_at)
when created_at then 'created_at'
when newstage_created_at then 'newstage_created_at'
when newstage_updated_at then 'newstage_updated_at'
when newdata_created_at then 'newdata_created_at'
else null
end greatestcolumn
FROM people_count_view
WHERE
annonce_id='1'
and
(id,greatestcolumn) in (select id,greatestcolumn from people_count_view)
order by id
当然greatestcolumn
不是列名,这是一个错误的尝试
不确定我是否理解你的任务是否正确,但你可以拿走这段代码并修改(通常它看起来就像你想要得到的)。
with t as (
select id,
max(created_at)::timestamp as max_created_at,
max(newstage_created_at) as max_newstage_created_at,
max(newstage_updated_at) as max_newstage_updated_at,
max(newdata_created_at) as max_newdata_created_at
from test.people_count_view
GROUP BY id
order by id)
select
id,
greatest(max_created_at,
max_newstage_created_at,
max_newstage_updated_at,
max_newdata_created_at) as greatestcolumn,
case greatest(max_created_at,
max_newstage_created_at,
max_newstage_updated_at,
max_newdata_created_at)
when max_created_at then 'max_created_at'
when max_newstage_created_at then 'max_newstage_created_at'
when max_newstage_updated_at then 'max_newstage_updated_at'
when max_newdata_created_at then 'max_newdata_created_at'
end as greatestcolumn_name
from t;
output result
关于您的代码 - 请记住不要在 WHERE 条件下使用别名 (greatestcolumn)。就我而言,我将查询包装在 CTE 中以将其用作列名。其他方式 - 在 WHERE 块中,您需要使用与此别名
在 CASE 中使用的相同条件
我使用 postgres v14, 我创建了一个包含 5 个时间列的 postgres 视图。
每个用户 ID 在第一列都有一个日期
表示每个userid超过4行
我想获得最大的一排
我的列是 (id,created_at,newstage_created_at,newstage_updated_at,newdata_created_at) 看起来像
id | created_at | newstage_created_at | newstage_updated_at | newdata_created_at |
---|---|---|---|---|
1 | 2020-11-23 11:28:05 | 2020-11-23 11:28:05 | 2020-11-23 11:28:05 | null |
1 | 2020-11-23 11:28:05 | 2020-11-23 11:28:05 | 2020-11-24 08:13:48 | 2020-11-25 08:13:48 |
1 | 2020-11-23 11:28:05 | 2020-10-11 05:55:36 | 2021-01-11 05:55:36 | 2020-10-11 05:55:36 |
2 | 2022-02-09 18:41:42 | 2022-02-09 18:41:42 | 2022-02-09 18:41:42 | 2022-02-09 18:41:42 |
2 | 2022-02-09 18:41:42 | 2022-02-09 18:41:42 | 2022-02-09 18:41:42 | null |
2 | 2022-02-09 18:41:42 | 2020-10-12 09:55:31 | 2022-02-09 18:41:42 | 2022-02-09 18:41:42 |
2 | 2022-02-09 18:41:42 | 2020-10-12 09:55:31 | 2022-02-09 18:41:42 | 2022-02-09 18:41:42 |
这里我需要得到具有最大列和最大行的行
我做了这个sql
case greatest(created_at, newstage_created_at ,newstage_updated_at,newdata_created_at)
when created_at then 'created_at'
when newstage_created_at then 'newstage_created_at'
when newstage_updated_at then 'newstage_updated_at'
when newdata_created_at then 'newdata_created_at'
else null
end greatestcolumn
FROM people_count_view
WHERE
annonce_id='1'
and
(id,greatestcolumn) in (select id,greatestcolumn from people_count_view)
order by id
当然greatestcolumn
不是列名,这是一个错误的尝试
不确定我是否理解你的任务是否正确,但你可以拿走这段代码并修改(通常它看起来就像你想要得到的)。
with t as (
select id,
max(created_at)::timestamp as max_created_at,
max(newstage_created_at) as max_newstage_created_at,
max(newstage_updated_at) as max_newstage_updated_at,
max(newdata_created_at) as max_newdata_created_at
from test.people_count_view
GROUP BY id
order by id)
select
id,
greatest(max_created_at,
max_newstage_created_at,
max_newstage_updated_at,
max_newdata_created_at) as greatestcolumn,
case greatest(max_created_at,
max_newstage_created_at,
max_newstage_updated_at,
max_newdata_created_at)
when max_created_at then 'max_created_at'
when max_newstage_created_at then 'max_newstage_created_at'
when max_newstage_updated_at then 'max_newstage_updated_at'
when max_newdata_created_at then 'max_newdata_created_at'
end as greatestcolumn_name
from t;
output result
关于您的代码 - 请记住不要在 WHERE 条件下使用别名 (greatestcolumn)。就我而言,我将查询包装在 CTE 中以将其用作列名。其他方式 - 在 WHERE 块中,您需要使用与此别名
在 CASE 中使用的相同条件