我希望能够unnest()
在一个复杂的SQL查询中使用PostgreSQL中的函数,该查询有很多JOIN
s.这是示例查询:
SELECT 9 as keyword_id, COUNT(DISTINCT mentions.id) as total, tags.parent_id as tag_id
FROM mentions
INNER JOIN taggings ON taggings.mention_id = mentions.id
INNER JOIN tags ON tags.id = taggings.tag_id
WHERE mentions.taglist && ARRAY[9] AND mentions.search_id = 3
GROUP BY tags.parent_id
Run Code Online (Sandbox Code Playgroud)
我想在taggings
这里删除表,因为我的mentions
表有一个名为taglist的整数数组字段,它包含所有链接的标记ID .mentions
我试过以下:
SELECT 9 as keyword_id, COUNT(DISTINCT mentions.id) as total, tags.parent_id as tag_id
FROM mentions
INNER JOIN tags ON tags.id IN (SELECT unnest(taglist))
WHERE mentions.taglist && ARRAY[9] …
Run Code Online (Sandbox Code Playgroud) unnest()
在扩展数组后进行转换时,我偶然发现了的异常行为。
使用unnest()有三种基本语法变体:
1)SELECT unnest('{1,NULL,4}'::int[]) AS i;
2)SELECT i FROM unnest('{2,NULL,4}'::int[]) AS i;
3)SELECT i FROM (SELECT unnest('{3,NULL,4}'::int[])) AS t(i);
它们都包括一行,NULL
结果符合预期
i
---
1
(null)
4
Run Code Online (Sandbox Code Playgroud)
要将数组元素转换为其他类型,可以在扩展数组后立即将元素转换为基本类型,或者在扩展之前将数组本身转换为其他数组类型。第一个变体对我来说似乎更简单或更短:
A)SELECT unnest('{4,NULL,1}'::int[])::text;
B)SELECT unnest('{4,NULL,2}'::int[]::text[]);
i
---
4
(null)
1
Run Code Online (Sandbox Code Playgroud)
2A)
由于某些原因,无法2)
与A)
SELECT * FROM unnest('{2,NULL,1}'::int[])::text;
Run Code Online (Sandbox Code Playgroud)
错误:“ ::”或附近的语法错误
我可以接受。由于某种原因未实施的罕见的极端情况。
所有其他组合都会飞,但是:
1A)SELECT unnest('{1,NULL,1}'::int[])::text AS i;
2A)SELECT i FROM unnest('{2,NULL,1}'::int[])::text AS i;
3A) …
给定:{{1,"a"},{2,"b"},{3,"c"}}
期望:
foo | bar
-----+------
1 | a
2 | b
3 | c
Run Code Online (Sandbox Code Playgroud)
您可以通过以下查询获得预期结果;但是,最好有一些可以随数组大小缩放的东西。
SELECT arr[subscript][1] as foo, arr[subscript][2] as bar
FROM ( select generate_subscripts(arr,1) as subscript, arr
from (select '{{1,"a"},{2,"b"},{3,"c"}}'::text[][] as arr) input
) sub;
Run Code Online (Sandbox Code Playgroud) 我正在使用plpgsql创建一个存储过程,通过传递一个类型数组并在过程中执行循环,以便我可以插入每个信息类型
CREATE TYPE info AS(
name varchar,
email_add varchar,
contact_no varchar
);
CREATE OR REPLACE FUNCTION insert_info(
info_array info[]
) RETURNS varchar AS $$
DECLARE
info_element info;
BEGIN
FOREACH info_element IN ARRAY info_array
LOOP
INSERT INTO info_table(
name,
email_add,
contact_no
) VALUES(
info_element.name,
info_element.email_add,
info_element.contact_no
);
END LOOP;
RETURN 'OK';
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何使用数组输入的函数.我做了一些实验(只有一些愚蠢的输入):
SELECT insert_info(
ARRAY[('Arjay','myEmail@email.com','1234567')]
);
Run Code Online (Sandbox Code Playgroud)
但PostgreSQL说这是一个record[]
,我还没有测试Loop部分...
我在这个链接中发现了一个类似的问题:
使用%TYPE在PostgreSQL中声明复合类型的变量,
但它没有使用数组.如果这只是一个重复的问题,也许你们可以指出我正确的方向!
我正在实现一个Query系统.我实现了不需要的功能.现在用户询问在单个select语句中使用多个unfst.我使用PostgreSQL作为指南,因为大多数用户在我们的查询系统之前使用它.
PostgreSQL有这样奇怪的行为:
postgres=# select unnest(array[1,2]), unnest(array[1,2]);
unnest | unnest
--------+--------
1 | 1
2 | 2
(2 rows)
postgres=# select unnest(array[1,2]), unnest(array[1,2,3]);
unnest | unnest
--------+--------
1 | 1
2 | 2
1 | 3
2 | 1
1 | 2
2 | 3
(6 rows)
Run Code Online (Sandbox Code Playgroud)
我的实现总是生成笛卡尔积.我想知道,这背后的正确逻辑是什么?PostgreSQL正在做正确的事还是只是一个bug?我没有在ANSI文档或PostgreSQL文档中找到明确的描述.
假设我有以下表格:
table: followers_arrays
id | array
--------+---------
1 | {3,4,5}
table: small_profiles
id | username | pic
--------+----------+-------
3 | aaaa | abcd
4 | bbbb | abcd
5 | cccc | abcd
Run Code Online (Sandbox Code Playgroud)
我想打印followers_array与填充的数据来自small_profiles使用简单连接。
首先,我使用这样的unnest函数:
SELECT id, unnest(followers_array) AS elem FROM followers_arrays
Run Code Online (Sandbox Code Playgroud)
它给了我正确的结果:
id | elem
--------+--------
1 | 3
1 | 4
1 | 5
Run Code Online (Sandbox Code Playgroud)
现在,从我的理解我只需要这个数据加盟small_profiles ON small_profiles.id这样的关键:
SELECT id, unnest(followers_array) AS elem
FROM followers_arrays
JOIN small_profiles ON small_profiles.instagram_id = elem …
Run Code Online (Sandbox Code Playgroud) 自从更新到tidyr
版本 1.0.0 以来,我在取消嵌套数据帧列表时开始出现错误。
出现错误的原因是列表中的某些数据帧包含具有所有 NA 值(逻辑)的列,而其他数据帧包含相同的列但具有一些字符值(字符)。具有所有 NA 值的列被编码为逻辑值,而其他列被编码为字符向量。
早期版本的默认行为tidyr
处理不同的列类型没有问题(至少我在运行脚本时没有收到此错误)。
我可以从内部解决这个问题tidyr::unest()
吗?
可重现的例子:
library(tidyr)
a <- tibble(
value = rnorm(3),
char_vec = c(NA, "A", NA))
b <- tibble(
value = rnorm(2),
char_vec = c(NA, "B"))
c <- tibble(
value = rnorm(3),
char_vec = c(NA, NA, NA))
tibble(
file = list(a, b, c)) %>%
unnest(cols = c(file))
#> No common type for `..1$file$char_vec` <character> and `..3$file$char_vec`
#> <logical>.
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.3.0)于 2019-10-11 创建
如果可能,我想bigrquery
使用dplyr
语法(而不是 SQL)探索 Google Analytics 360 数据。要点是我想了解用户旅程——我有兴趣在用户级别(甚至跨会话)找到最常见的页面序列。
我以为我可以这样做:
sample_query <- ga_sample %>%
select(fullVisitorId, date, visitStartTime, totals, channelGrouping,
hits.page.pagePath) %>%
collect()
Run Code Online (Sandbox Code Playgroud)
但我收到一个hits.page.pagePath
未找到的错误。然后我尝试:
sample_query <- ga_sample %>%
select(fullVisitorId, date, visitStartTime, totals, channelGrouping, hits) %>%
collect() %>%
unnest_wider(hits)
Run Code Online (Sandbox Code Playgroud)
但结果是Error: Requested Resource Too Large to Return [responseTooLarge]
,这是完全有道理的。
从我收集到的信息来看,使用 SQL 语法,解决方法是unnest
远程访问,并且select
只有hits.page.pagePath
字段(而不是整个hits
顶级字段)。
例如,这样的事情(这是一个不同的查询,但传达了这一点):
SELECT
hits.page.pagePath
FROM
'bigquery-public-data.google_analytics_sample.ga_sessions_20160801' AS GA,
UNNEST(GA.hits) AS hits
GROUP BY
hits.page.pagePath
Run Code Online (Sandbox Code Playgroud)
是否可以用dplyr
语法做类似的事情?如果不可能,那么使用 SQL 的最佳方法是什么?
谢谢! …
我想问如何取消列表列表的嵌套并将其转换为数据框的不同列。具体来说,我有以下数据框,其中 Route_setcolumn
是列表的列表:
Generation Route_set
0 0 [[20. 19. 47. 56.] [21. 34. 78. 34.]]
Run Code Online (Sandbox Code Playgroud)
所需的输出是以下数据帧:
route1 route2
0 20 21
1 19 34
2 47 78
3 56 34
Run Code Online (Sandbox Code Playgroud)
我有什么想法可以做到吗?先感谢您!
我在 Athena 查询中使用unnest
更多的扁平化多个数组。当数组有一些记录时,它返回正确的结果。但是当第二个数组为空时,它不会返回任何记录。有人可以让我知道如何取消嵌套以在单个查询中取消嵌套多个数组吗?
以下查询返回空行。
WITH example AS (
SELECT devop, devs
FROM
UNNEST(ARRAY['Sharon', 'John', 'Bob', 'Sally']) AS t(devop),
UNNEST(ARRAY[]) AS t(devs)
)
select array_join(array_agg(distinct example.devop),';'),array_join(array_agg(distinct example.devs),';') from example
Run Code Online (Sandbox Code Playgroud)
以下查询返回正确的结果。
WITH example AS (
SELECT devop, devs
FROM
UNNEST(ARRAY['Sharon', 'John', 'Bob', 'Sally']) AS t(devop),
UNNEST(ARRAY['a','b']) AS t(devs)
)
select array_join(array_agg(distinct example.devop),';'),array_join(array_agg(distinct example.devs),';') from example
Run Code Online (Sandbox Code Playgroud)
当第二个数组为空时我想要以下结果
_col0 _col1
----------------------------------------------
Sally;John;Bob;Sharon
Run Code Online (Sandbox Code Playgroud)