在 PL/SQL 中,假设我有一些定义如下的关联数组:
declare
type a_arr_t is table of PLS_INTEGER index by PLS_INTEGER;
a_arr a_arr_t;
Run Code Online (Sandbox Code Playgroud)
然后,我按如下方式稀疏地填充数组:
begin
a_arr(1) := 2;
a_arr(10) := 4;
a_arr(100) := 6;
end;
Run Code Online (Sandbox Code Playgroud)
是否有一些运算符或函数可以为我提供数组的索引,(1,10,100)作为某种集合,例如indices of在forall语句中?
我正在尝试运行以下 sql 命令:
SELECT ARRAY(
SELECT column_name
FROM information_schema.columns
WHERE table_name ='gis_field_configuration_stage'
);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ERROR: could not find array type for datatype information_schema.sql_identifier
Run Code Online (Sandbox Code Playgroud) 有什么方法可以对 postgresql 中的数组元素进行模式匹配(9.4,如果版本有所不同)?我有一个聚合函数,除其他外,它返回一个元素数组,例如:
SELECT
lognum
,array_agg(flightnum) as flightnums
FROM logs
GROUP BY lognum;
Run Code Online (Sandbox Code Playgroud)
其中flightnum字段是包含文本字符串或三或四位数字的 varchar。现在假设我要选择航班号以“8”开头的所有日志(即“800”或“8000”系列航班)。我的第一个想法是做这样的事情:
SELECT
*
FROM (
SELECT
lognum
,array_agg(flightnum) as flightnums
FROM logs
GROUP BY
lognum
) s1
WHERE
'8%' like ANY(flightnums);
Run Code Online (Sandbox Code Playgroud)
但是,虽然这不会给出错误,但它也不会返回任何结果。我猜测这是因为通配符位于运算符的左侧。当然,反过来就是:
WHERE ANY(flightnum) like '8%'
Run Code Online (Sandbox Code Playgroud)
给我一个语法错误。那么有什么方法可以运行此查询,以便获得包含以 8(或其他)开头的航班号的任何行吗?
请注意,这是一个简化的示例,仅演示了我遇到困难的部分。
我正在尝试LEFT JOIN使用数组unnest()函数实现类似的功能。如果数组为空,我希望查询返回具有空值的行。因此,通过使用CASE构造,如果源数组为空,我想传递具有单个空元素的假数组,但它无法按预期工作:
查询 1
select element
from (
select array['a']::text[] as arr --< single non-null element
) sub, unnest(
(
case when array_length(sub.arr, 1) <= 0 then (array[null])::text[]
else sub.arr
end
)
) element
-- returns 1 row with element = "a"
Run Code Online (Sandbox Code Playgroud)
查询 2
select element
from (
select array[]::text[] as arr --< empty array
) sub, unnest(
(
case when array_length(sub.arr, 1) <= 0 then (array[null])::text[]
else sub.arr
end
)
) element
-- …Run Code Online (Sandbox Code Playgroud) 与相同数据类型的普通列相比,数组的额外开销是多少?换句话说,如果一个数组几乎总是有一个值,那么使用数组而不是普通列会“浪费”多少空间?
我们有一个简单的语法,允许我们查看单个标量的数组,
SELECT 'foo' = ANY(ARRAY['foo', 'bar', 'baz']);
Run Code Online (Sandbox Code Playgroud)
我们可以用同样的方法来匹配 LIKE
SELECT 'foobar' LIKE ANY(ARRAY['foo%', 'bar%', 'baz%'];
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果你想做另一个。
SELECT ANY(ARRAY['foobar', 'barbar', 'bazbar']) LIKE 'foo%'
ERROR: syntax error at or near "ANY"
LINE 1: SELECT ANY(ARRAY['foobar', 'barbar', 'bazbar']) LIKE 'foo%';
Run Code Online (Sandbox Code Playgroud)
我知道语法不起作用,但我希望它起作用。
# SELECT ARRAY['foobar', 'barbar', 'bazbar'] LIKE 'foo%';
ERROR: operator does not exist: text[] ~~ unknown
LINE 1: SELECT ARRAY['foobar', 'barbar', 'bazbar'] LIKE 'foo%';
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)
我想过滤一个数组以查看元素是否存在。这可能不使用 …
如何将texts 数组转换为UUIDs数组?
我需要join在两个表之间做一个:users和projects。
该users表有一个名为的数组字段,project_ids其中包含作为文本的项目 ID。
该projects表有一个名为 的 UUID 字段id。
我最初的想法是一个查询看起来像:
SELECT * FROM projects
JOIN users ON
projects.id = ANY(users.project_ids)
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为users.project_ids不是,UUID所以我试过:
projects.id = ANY(users.project_ids::uuid[])
Run Code Online (Sandbox Code Playgroud)
乃至:
projects.id = ANY(ARRAY[users.project_ids]::uuid[])
Run Code Online (Sandbox Code Playgroud)
但没有一个有效:
Run Code Online (Sandbox Code Playgroud)ERROR: invalid input syntax for type uuid: ""
更新
@a_horse_with_no_name 绝对正确。最好的选择应该是使用一组 UUID。
现在的问题是我如何可以改变的阵列text到阵列uuid?
该users表当前为空(0 条记录)。
我试过了
ALTER TABLE "users" ALTER COLUMN "project_ids" SET …Run Code Online (Sandbox Code Playgroud) 我发现:
但是,我无法将其用于我的案例。
我有一个像这样的表(实际myid值是散列,但为了说明在这里简化了):
create temp table a (myid text, ip inet);
insert into a (myid, ip)
values
('0a', '10.10.1.1'),
('0a', '10.10.1.2'),
('0a', '10.10.1.3'),
('0b', '10.10.1.2'),
('0b', '10.10.1.4'),
('0c', '10.10.1.5'),
('0d', '10.10.1.3'),
('0e', '10.10.1.6'),
('0e', '10.10.1.7'),
('0f', '10.10.1.8'),
('0f', '10.10.1.9'),
('10', '10.10.1.9'),
('11', '10.10.1.10'),
('12', '10.10.1.11'),
('12', '10.10.1.4'),
('1a', '10.10.1.2'),
('1a', '10.10.1.4'),
('1e', '10.10.1.11'),
('1f', '10.10.1.12'),
('23', '10.10.1.12');
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何产生的结果是:
ids | ips
---------------------+------------------------------------------------------
{0a,0b,0d,12,1a,1e} | {10.10.1.1,10.10.1.2,10.10.1.3,10.10.1.4,10.10.1.11}
{0c} | {10.10.1.5}
{0e} | {10.10.1.6,10.10.1.7}
{0f,10} | {10.10.1.8,10.10.1.9}
{11} | …Run Code Online (Sandbox Code Playgroud) 当特定图书馆中没有书籍数据时,就会出现问题。考虑以下工作场景。
桌子 library
--------------------------------
| id | name | owner |
--------------------------------
| 1 | ABC | A |
| 2 | DEF | D |
| 3 | GHI | G |
--------------------------------
Run Code Online (Sandbox Code Playgroud)
桌子 books
--------------------------------
| id | title | library |
--------------------------------
| a | xxx | 1 |
| b | yyy | 1 |
| c | zzz | 2 |
--------------------------------
Run Code Online (Sandbox Code Playgroud)
现在,当我进行如下查询时:
SELECT library.name, array_agg(b.title) AS book_list FROM library,
(SELECT title FROM books WHERE …Run Code Online (Sandbox Code Playgroud) 这里我们有两组数字。问题是我无法弄清楚如何从数字的输入到输出(下面的 DDL 和 DML 以及这里的小提琴)。
current_data
1,2,3
1,4
1,5,7
8,9,10
10,11,15
expected_outcome
1,2,3,4,5,7
8,9,10,11,15
Run Code Online (Sandbox Code Playgroud)
我们只是尝试根据单个数字是否与任何其他组匹配来匹配一组数字。然后合并所有这些组。
例如。
如果我们有:
('1,2,3'),
('1,4'),
('1,5,7')
Run Code Online (Sandbox Code Playgroud)
我们想要:
(1,2,3,4,5,7)
Run Code Online (Sandbox Code Playgroud)
我们将它们合并为 PostgreSQL 中的一行。
或(另一个例子):
('8,9,10'),
('10,11,15')
Run Code Online (Sandbox Code Playgroud)
所需的输出:
(8,9,10,11,15)
Run Code Online (Sandbox Code Playgroud)
查询将对这些数字进行分组,因为它们的共同点是数字 10。但它不会与(1,2,3,4,5,7)不共享数字的前一行(即)分组。
当我们将这些组放在一张表中时。如果他们在每一组中至少有一个匹配的号码,他们才会分组在一起。
======== DDL 和 DML ============
create table current (current_data text not null);
create table expected_output (expected_outcome text not null);
insert into current (current_data) values ('1,2,3'),('1,4'),('1,5,7'),('8,9,10'), ('10,11,15');
insert into expected_output (expected_outcome) values ('1,2,3,4,5,7'),('8,9,10,11,15');
Run Code Online (Sandbox Code Playgroud)