- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 locations
的 Postgres 表。它有几百万行数据,格式如下
id | location_a | location_b
----+--------------+--------------
36 | Sydney | London
37 | Atlanta | London
38 | New York | Tokyo
39 | Tokyo | Sydney
40 | Tokyo | Sydney
.....
我希望能够生成以下形式的数据透视表/计数 -
问题是列数是可变的,因此必须以编程方式/动态确定,而不是使用静态 SELECT
查询。
我理解 PL/pgSQL 的基本概念,因为它是一种脚本语言,可以让我做这样的动态事情。
但是我在开始时遇到了很多麻烦。有没有简单的方法来计算以上内容?
最佳答案
您可以动态创建 View 。描述了比您的案例更简单的想法和解决方案 in this answer.请在继续之前阅读它。
我们将使用以下查询来创建 View :
with all_locations(location) as (
select distinct location_a
from locations
union
select distinct location_b
from locations
)
select location_a as location, json_object_agg(location_b, count order by location_b) as data
from (
select a.location as location_a, b.location as location_b, count(l.*)
from all_locations a
cross join all_locations b
left join locations l on location_a = a.location and location_b = b.location
group by 1, 2
) s
group by 1
order by 1;
结果:
location | data
----------+----------------------------------------------------------------------------
Atlanta | { "Atlanta" : 0, "London" : 1, "New York" : 0, "Sydney" : 0, "Tokyo" : 0 }
London | { "Atlanta" : 0, "London" : 0, "New York" : 0, "Sydney" : 0, "Tokyo" : 0 }
New York | { "Atlanta" : 0, "London" : 0, "New York" : 0, "Sydney" : 0, "Tokyo" : 1 }
Sydney | { "Atlanta" : 0, "London" : 1, "New York" : 0, "Sydney" : 0, "Tokyo" : 0 }
Tokyo | { "Atlanta" : 0, "London" : 0, "New York" : 0, "Sydney" : 2, "Tokyo" : 0 }
(5 rows)
城市列表将在函数中使用两次,因此它存储在数组cities
中。请注意,您可以将函数中的第一个查询替换为更简单的查询(它只是不同城市的有序列表)。
create or replace function create_locations_view()
returns void language plpgsql as $$
declare
cities text[];
list text;
begin
-- fill array with all cities in alphabetical order
select array_agg(location_a order by location_a)
from (
select distinct location_a
from locations
union
select distinct location_b
from locations
) s
into cities;
-- construct list of columns to use in select list
select string_agg(format($s$data->>'%1$s' "%1$s"$s$, city), ', ')
from unnest(cities) city
into list;
-- create view from select based on the above list
execute format($ex$
drop view if exists locations_view;
create view locations_view as
select location, %1$s
from (
select location_a as location, json_object_agg(location_b, count order by location_b) as data
from (
select a.location as location_a, b.location as location_b, count(l.*)
from unnest(%2$L::text[]) a(location)
cross join unnest(%2$L::text[]) b(location)
left join locations l on location_a = a.location and location_b = b.location
group by 1, 2
) s
group by 1
) s
order by 1
$ex$, list, cities);
end $$;
使用该函数并从创建的 View 中选择数据:
select create_locations_view();
select * from locations_view;
location | Atlanta | London | New York | Sydney | Tokyo
----------+---------+--------+----------+--------+-------
Atlanta | 0 | 1 | 0 | 0 | 0
London | 0 | 0 | 0 | 0 | 0
New York | 0 | 0 | 0 | 0 | 1
Sydney | 0 | 1 | 0 | 0 | 0
Tokyo | 0 | 0 | 0 | 2 | 0
(5 rows)
我多次使用这种方法,但我没有处理过真正的大数据,所以我不能保证它是有效的。
关于sql - 如何使用 PL/pgSQL 构建具有动态列的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45993046/
PL/1 中有许多不同的数字数据类型。我想知道什么时候有整数除法,什么地方没有。暂时,我写了一个小例子来说明(至少对我而言)PL/1 非常纠结于其中: DCL BIN15 FIXED BIN(15)
我是 Prolog 的新手。我有两个文件。其中之一是“names.pl”,另一个是“verbs.pl”。这两个文件都有事实。 “names.pl”有关于很多名词等的事实。事实的名字是关系。 这些文件的
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 3 年前。
我正在处理一个存储的 PL/SQL 函数,该函数根据给定的员工编号查找员工的家属姓名。到目前为止,我能够获得所需的输出,但在输出期间似乎有逗号问题。对于如何在输出过程中删除最后一个括号,我们将不胜
我观察到有两种执行 perl 程序的方法: perl test.pl 和 ./test.pl 这两者之间的确切区别是什么,哪一个值得推荐? 最佳答案 我将稍微改写其他答案所说的内容。 第一种情况将运行
我有一个表 TDATAMAP,其中包含大约 1000 万条记录,我想将所有记录提取到 PL/SQL 表类型变量中,将其与某些条件进行匹配,最后将所有必需的记录插入临时表中。请告诉我是否可以使用 PL/
一切都在标题中。 我在游标上循环并想要 EXIT WHEN curs%NOTFOUND 当没有更多行时,PostgreSQL 下的 %NOTFOUND 等同于什么? 编辑 或其他游标属性 %ISOPE
CREATE FUNCTION foo() RETURNS text LANGUAGE plperl AS $$ return 'foo'; $$; CREATE FU
我正在使用 ack.pl 工具来搜索文件中的字符串或 IP ack.pl 的官方网站是 - http://beyondgrep.com/documentation/ ack.pl CLI 示例(想在/
代码 #!/usr/bin/perl -I/root/Lib/ use Data::Dumper; print Dumper \@INC; 以上代码文件名为test.pl,权限为755。 当我使用/u
编写一个 PL/SQL 过程,将员工编号和薪水作为输入参数,并从经理为 'BLAKE' 且薪水在 1000 到 2000 之间的员工表中删除。 我写了下面的代码:- create or replac
我需要对更新行进行一些审核。 所以我有一个函数,它接收 some_table%ROWTYPE 类型的参数,其中包含要为该行保存的新值。 我还需要在历史表中保存一些有关更改的列值的信息。我正在考虑从 a
如果我在 PL/SQL 存储过程中使用许多 CLOB 变量来存储许多长字符串,是否存在性能问题? CLOB 的长度也是可变的吗? CLOB 代替使用 varchar2 和 long 是否有任何已知的限
我想使用 JavaScript/Apex 创建一个按钮,这样当我点击它时,就会“调用”一个 PL-SQL 过程。与常规 html 按钮类似,但 onClick="JavaScript function
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
今天的好时间,想问问有没有人知道在IBM Bluemix 云上安装PostgreSQL 扩展(准确的说是pl/r 和pl/python)的方法是什么?我在那里运行 compose-postgresql
是否可以像普通 Python 函数一样从其他 PL/Python block 调用 PL/Python 函数。 例如,我有一个函数f1: create or replace function f1()
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我正在使用返回 REF CURSOR 的 Java 在 PL/SQL 中调用存储函数: FUNCTION getApprovers RETURN approvers_cursor IS app
通过终端修改Webmin密码时 Can't locate ./acl/md5-lib.pl at /usr/share/webmin/changepass.pl 使用 Ubuntu 20 最佳答案 U
我是一名优秀的程序员,十分优秀!