我有一张 table :
id; user_id; field_name; value; timestamp
1 1 user_status PENDING 2018-07-31 14:20:28.632838
2 1 selfie APPROVED 2018-07-31 14:20:29.632838
3 2 user_status PENDING 2018-07-31 14:20:31.632838
4 1 passport APPROVED 2018-07-31 14:20:33.632838
5 3 user_status PENDING 2018-07-31 14:20:44.632838
6 2 selfie REJECTED 2018-07-31 14:20:52.632838
7 1 user_status ACTIVATED 2018-07-31 14:20:54.632838
我尝试从以下值中共同选择列:最新的 user_status、最新的自拍、最新的按用户 ID 分组的护照:
预期表:
user_id; user_status; selfie; passport
1 ACTIVATED APPROVED APPROVED
2 PENDING REJECTED [null]
3 PENDING [null] [null]
我使用了这个 sql:
select
user_id,
case when field_name='user_status' then value end as user_status,
case when field_name='selfie' then value end as selfie,
case when field_name='passport' then value end passport
from user_history
group by user_id, field_name, value
但它给出了一个具有空值且没有不同 user_id 的表。
user_id; user_status; selfie; passport
1 PENDING [null] [null]
3 PENDING [null] [null]
1 ACTIVATED [null] [null]
2 [null] REJECTED [null]
1 [null] APPROVED [null]
2 PENDING [null] [null]
1 [null] [null] APPROVED
有什么想法吗?谢谢。
最佳答案
第一个获取最新值的 CTE:
db=# select distinct user_id, field_name, first_value(value) over (partition by user_id, field_name order by timestamp desc) from user_history;
user_id | field_name | first_value
---------+-------------+-------------
1 | selfie | APPROVED
1 | passport | APPROVED
2 | selfie | REJECTED
3 | user_status | PENDING
1 | user_status | ACTIVATED
2 | user_status | PENDING
(6 rows)
第二次(从您的尝试中)获得人造柱:
db=# with c as (select distinct user_id, field_name, first_value(value) over (partition by user_id, field_name order by timestamp desc) from user_history)
select distinct user_id
, case when field_name = 'user_status' then first_value end
, case when field_name = 'selfie' then first_value end
, case when field_name = 'passport' then first_value end
from c
order by user_id;
user_id | case | case | case
---------+-----------+----------+----------
1 | ACTIVATED | |
1 | | APPROVED |
1 | | | APPROVED
2 | PENDING | |
2 | | REJECTED |
3 | PENDING | |
(6 rows)
终于崩溃了:
db=# with c as (select distinct user_id, field_name, first_value(value) over (partition by user_id, field_name order by timestamp desc) from user_history)
, ct as (
select distinct user_id
, case when field_name = 'user_status' then first_value end user_status
, case when field_name = 'selfie' then first_value end selfie
, case when field_name = 'passport' then first_value end passport
from c
)
select
user_id
, max(user_status) user_status
, max(selfie)selfie
, max(passport) passport
from ct
group by user_id
order by user_id;
user_id | user_status | selfie | passport
---------+-------------+----------+----------
1 | ACTIVATED | APPROVED | APPROVED
2 | PENDING | REJECTED |
3 | PENDING | |
(3 rows)
关于sql - Postgresql,从行中选择值作为不带空的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51609102/