Postgres:将列值提取到行标题和值中

huangapple go评论71阅读模式
英文:

Postgres: pull column value into row header and value

问题

使用Postgres 12.8。我的表以以下方式存储单个流程实例的变量:

我需要为每个ID创建单行,以便列名将是变量名(例如:appealDto、offshoreOnly、dueDate等),值将是变量值。因此,它应该如下所示:

id_                 |        offshoreOnly      |     dueDate
------------------------------------------------------------
ecb-sad-23132       |      0                    |16356454656

请注意,此表以一种有趣的方式存储变量值。它根据变量的类型使用3个列(double_、long_、text_)中的一个来存储变量值。

我尝试过,但我能做到的最好的是:

select v.id_ proc_id , 
    (case when v.name_='dueDate' then v.long_ else null end) due_date,
    (case when v.name_='offShoreOnly' then v.long_ else null end) offShoreOnly
from camunda_data.act_ru_variable v 

但这没有给我预期的结果。

我还尝试过:

select v.proc_inst_id_::text , 
     v.name_::text,
    v.long_::text
from camunda_data.act_ru_variable v 
where v.proc_def_id_ NOT ILIKE 'StartProcess%'
and v.name_ in ('dueDate','offShoreOnly')

但这给了我:

希望有建议?提前感谢。

英文:

Using Postgres 12.8. My table stores variables for a single process instance in this way:

Postgres:将列值提取到行标题和值中

I need to create single row per id so that the column name will be the variable name (for example: appealDto, offshoreOnly, dueDate etc) and the value will be the variable value. So, it should look like:

id_                 |        offshoreOnly      |     dueDate
------------------------------------------------------------
ecb-sad-23132       |      0                    |16356454656

Notice that this table has an interesting way of storing variable value. It uses 1 of the 3 columns (double_, long_, text_) to store the variable-value depending on the variable's type.

I tried but best I could do:

select v.id_ proc_id , 
    (case when v.name_='dueDate' then v.long_ else null end) due_date,
    (case when v.name_='offShoreOnly' then v.long_ else null end) offShoreOnly
from camunda_data.act_ru_variable v 

And that did not give me the expected result.

Also tried:

select v.proc_inst_id_::text , 
     v.name_::text,
    v.long_::text
from camunda_data.act_ru_variable v 
where v.proc_def_id_ NOT ILIKE 'StartProcess%'
and v.name_ in ('dueDate','offShoreOnly')

But that gave me :

Postgres:将列值提取到行标题和值中

Any suggestion? Thanks in advance

答案1

得分: 0

你可能需要的内容:

CREATE EXTENSION tablefunc SCHEMA camunda_data;

而最终有效的查询是:

select * from crosstab (
$$select v.proc_inst_id_::text , 
     v.name_::text,
    v.long_::text
from camunda_data.act_ru_variable v 
where v.proc_def_id_ NOT ILIKE 'StartAppealProcess%'
and v.name_ in ('dueDate','offShoreOnly')$$)
as (proc_id text, due_date text, offshore text);
英文:

You may need :

CREATE EXTENSION tablefunc SCHEMA camunda_data;

And the query that finally worked:

select * from crosstab (
$$select v.proc_inst_id_::text , 
     v.name_::text,
    v.long_::text
from camunda_data.act_ru_variable v 
where v.proc_def_id_ NOT ILIKE 'StartAppealProcess%'
and v.name_ in ('dueDate','offShoreOnly')$$)
as (proc_id text, due_date text, offshore text);

huangapple
  • 本文由 发表于 2023年5月31日 23:09:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76374949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定