gpt4 book ai didi

postgresql - 如何使用动态字段在 PostgreSQL 中进行透视/交叉表

转载 作者:行者123 更新时间:2023-11-29 12:21:26 24 4
gpt4 key购买 nike

我是 PostgreSQL 新手。

id | customer_id | form_id | field_id | field_name | form_submission_id |    value     |
---+-------------+---------+----------+------------+--------------------+--------------+
1 | 2 | 7 | c313 | Program | 1 | 2013 |
2 | 2 | 7 | c313 | Program | 2 | PIP |
3 | 2 | 7 | c313 | Program | 3 | CIP |
4 | 2 | 7 | c343 | Broker | 1 | broker test |
5 | 2 | 7 | c343 | Broker | 2 | broker test1 |
6 | 2 | 7 | c343 | Broker | 3 | broker test2 |
7 | 2 | 7 | c339 | Class | 1 | Class test |
8 | 2 | 7 | c339 | Class | 2 | Class test1 |
9 | 2 | 7 | c339 | Class | 3 | Class test2 |

我想要这样的记录

customer_id form_id Program      Broker        Class        form_submission_id 
2 7 2013 broker test Class test 1
2 7 PIP broker test1 Class test1 1
2 7 CIP broker test2 Class test3 1

field_name 值将是动态的,而不是固定值。

我已经尝试过了,但出现了类似“错误:返回和 sql 元组描述不兼容”的错误

select * from crosstab (
'select Distinct customer_id ,form_id , field_name from form_submissions_reports '
)
as newtable (
customer_id integer,form_id integer,field_id1 varchar,field_id2 varchar,field_id3 varchar
);

但重要的是它的字段名称是动态的。

最佳答案

设置示例:

create table form_submissions_reports (id integer, customer_id integer, 
form_id integer, field_id text, field_name text, form_submission_id integer,
"value" text);


insert into form_submissions_reports
select 1 ,2 ,7 , 'c313', 'Program',1 , '2013' union all
select 2 ,2 ,7 , 'c313', 'Program',2 , 'PIP' union all
select 3 ,2 ,7 , 'c313', 'Program',3 , 'CIP' union all
select 4 ,2 ,7 , 'c343', 'Broker',1 , 'broker test' union all
select 5 ,2 ,7 , 'c343', 'Broker',2 , 'broker test1' union all
select 6 ,2 ,7 , 'c343', 'Broker',3 , 'broker test2' union all
select 7 ,2 ,7 , 'c339', 'Class',1 , 'Class test' union all
select 8 ,2 ,7 , 'c339', 'Class',2 , 'Class test1' union all
select 9 ,2 ,7 , 'c339', 'Class',3 , 'Class test2';

这是您尝试过的固定列数的解决方案:

select * 
from crosstab (
'select Distinct form_submission_id , customer_id ,form_id , field_name, value
from form_submissions_reports order by form_submission_id ',
'select Distinct field_name from form_submissions_reports'
)as newtable (
form_submission_id integer, customer_id integer,form_id integer,field_id
varchar,field_id2 varchar,field_id3 varchar
);

就我的经验而言,关系数据库不是为列数及其类型不固定的类型构建的。所以你必须作弊。

本文仅供娱乐。我在这个完全“动态”的解决方案(灵活的列数、数据驱动的名称)中看不到任何生产值(value):

1) 创建这两个函数:

CREATE OR REPLACE FUNCTION foo()
RETURNS text as
$BODY$
DECLARE
dynamic_columns text;
BEGIN

select array_to_string(array_agg(distinct field_name||' text'), ', ') into dynamic_columns from form_submissions_reports;

return 'select * from crosstab (
''select Distinct form_submission_id , customer_id ,form_id , field_name, value from form_submissions_reports order by form_submission_id '',
''select Distinct field_name from form_submissions_reports''
)
as newtable (
form_submission_id integer, customer_id integer,form_id integer, '|| dynamic_columns ||'
)';


END;
$BODY$
LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION bar()
RETURNS void as
$BODY$
DECLARE
dyn_crosstab text;
BEGIN

DROP VIEW IF EXISTS barview;

select foo() into dyn_crosstab;

execute 'create view barview as '||dyn_crosstab;

END;
$BODY$
LANGUAGE plpgsql;

2) 执行 bar() 函数。这将为您提供新版本的“barview” View 。之后,查询barview。

select bar();
select * from barview;

3) 要测试它是否是动态的,插入一个具有不同 field_name 值的新行,然后重复 2):

INSERT INTO form_submissions_reports
(
id, customer_id, form_id, field_id,
field_name, form_submission_id, value
)
VALUES( 10, 2, 7, 'd', 'NEWFIELD', 4,
'newfield test');

select bar();
select * from barview;

关于postgresql - 如何使用动态字段在 PostgreSQL 中进行透视/交叉表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20611750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com