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

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

Postgres: pull column value into row header and value

问题

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

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

  1. id_ | offshoreOnly | dueDate
  2. ------------------------------------------------------------
  3. ecb-sad-23132 | 0 |16356454656

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

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

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

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

我还尝试过:

  1. select v.proc_inst_id_::text ,
  2. v.name_::text,
  3. v.long_::text
  4. from camunda_data.act_ru_variable v
  5. where v.proc_def_id_ NOT ILIKE 'StartProcess%'
  6. 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:

  1. id_ | offshoreOnly | dueDate
  2. ------------------------------------------------------------
  3. 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:

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

And that did not give me the expected result.

Also tried:

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

But that gave me :

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

Any suggestion? Thanks in advance

答案1

得分: 0

你可能需要的内容:

  1. CREATE EXTENSION tablefunc SCHEMA camunda_data;
  2. 而最终有效的查询是:
  3. select * from crosstab (
  4. $$select v.proc_inst_id_::text ,
  5. v.name_::text,
  6. v.long_::text
  7. from camunda_data.act_ru_variable v
  8. where v.proc_def_id_ NOT ILIKE 'StartAppealProcess%'
  9. and v.name_ in ('dueDate','offShoreOnly')$$)
  10. as (proc_id text, due_date text, offshore text);
英文:

You may need :

  1. CREATE EXTENSION tablefunc SCHEMA camunda_data;

And the query that finally worked:

  1. select * from crosstab (
  2. $$select v.proc_inst_id_::text ,
  3. v.name_::text,
  4. v.long_::text
  5. from camunda_data.act_ru_variable v
  6. where v.proc_def_id_ NOT ILIKE 'StartAppealProcess%'
  7. and v.name_ in ('dueDate','offShoreOnly')$$)
  8. 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:

确定