unnest

R: How to keep names while unnesting doubled nested tibble?

回眸只為那壹抹淺笑 提交于 2019-12-07 15:46:32
问题 at the moment I'm trying to figure out how to keep the names of an inner and other list nested within a tibble while unnesting. The .id parameter of the unnest function is the closest I found, but it starts to number the values instead of using the given names. here is a MWE with my idea of the final tibble: library(dplyr) library(tidyr) df.1 <- tibble( x = list("Foo","Bar"), y = list( list(a = list(aa = 1, ab = 2), b = list(ba = 6, bb = 22)), list(c = list(ca = 561, cb = 35), d = list(da =

Using UNNEST with a JOIN

陌路散爱 提交于 2019-12-06 03:15:49
问题 I want to be able to use unnest() function in PostgreSQL in a complicated SQL query that has many JOIN s. Here's the example query: 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 I want to eliminate the taggings table here, because my mentions table has

Postgres JOIN with unnest

≯℡__Kan透↙ 提交于 2019-12-06 03:11:02
问题 Assume I have following tables: table: followers_arrays id | array --------+--------- 1 | {3,4,5} table: small_profiles id | username | pic --------+----------+------- 3 | aaaa | abcd 4 | bbbb | abcd 5 | cccc | abcd I would like to print followers_array with populated data from small_profiles using simple JOINs. At first, I'm using unnest function like this: SELECT id, unnest(followers_array) AS elem FROM followers_arrays And it gives me about right result: id | elem --------+-------- 1 | 3 1

Using UNNEST with a JOIN

与世无争的帅哥 提交于 2019-12-04 07:22:46
I want to be able to use unnest() function in PostgreSQL in a complicated SQL query that has many JOIN s. Here's the example query: 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 I want to eliminate the taggings table here, because my mentions table has an integer array field named taglist that consists of all linked tag ids of mentions . I tried following

How get all matching positions in a string?

我与影子孤独终老i 提交于 2019-12-04 07:13:04
问题 I have a column flag_acumu in a table in PostgreSQL with values like: 'SSNSSNNNNNNNNNNNNNNNNNNNNNNNNNNNNSNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN' I need to show all positions with an 'S'. With this code, I only get the first such position, but not the later ones. SELECT codn_conce, flag_acumu, position('S' IN flag_acumu) AS the_pos FROM dh12 WHERE position('S' IN flag_acumu) != 0 ORDER BY the_pos ASC; How to get all of them? 回答1: Since you didn't specify your needs to a point in which one

IN Linux Distinct SQL is not working with UNNEST

别等时光非礼了梦想. 提交于 2019-12-02 11:16:47
When i run this query in window system behave correctly UNNSET but when i run this query Linux behave different.unnset duplicate record list on different row SELECT DISTINCT "billing_billmanagement"."creation_date", "billing_billmanagement"."bill_number", unnest(array_agg(DISTINCT "inventory_product"."product_name")) AS "product", unnest(array_agg(DISTINCT "services_service"."name")) AS "service" FROM "billing_billmanagement" INNER JOIN "users_staffuser" ON ("billing_billmanagement"."staff_id" = "users_staffuser"."id") INNER JOIN "auth_user" ON ("users_staffuser"."user_id" = "auth_user"."id")

UNNEST expression references column which is neither grouped nor aggregated

坚强是说给别人听的谎言 提交于 2019-12-01 21:49:46
问题 Google Analytics BigQuery tables are structured like this (Legacy SQL notations - only relevant fields are shown): visitId: INTEGER hits: RECORD/REPEATED hits.hour: INTEGER On one such table, the following query works well: SELECT visitId, MIN(h.hour) AS firstHitHour FROM `my-table.ga_sessions_20161122`, UNNEST(hits) AS h GROUP BY visitId But using this alternative syntax: SELECT visitId, (SELECT MIN(hour) FROM UNNEST(hits)) as firstHitHour FROM `my-table.ga_sessions_20161122` GROUP BY

NULL emements lost when casting result of unnest()

点点圈 提交于 2019-12-01 18:54:28
I stumbled upon very odd behavior with unnest() , when casting after expanding an array. Introduction There are three basic syntax variants to use 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); All of them include a row with NULL in the result as expected i --- 1 (null) 4 To cast the array elements to a different type, one can cast the elements to a basic type right after expanding the array, or cast the array itself to a different array type before expanding . The first

NULL emements lost when casting result of unnest()

孤街醉人 提交于 2019-12-01 18:09:41
问题 I stumbled upon very odd behavior with unnest(), when casting after expanding an array. Introduction There are three basic syntax variants to use 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); All of them include a row with NULL in the result as expected i --- 1 (null) 4 To cast the array elements to a different type, one can cast the elements to a basic type right after

using tidyr unnest with NULL values

混江龙づ霸主 提交于 2019-11-30 13:54:09
I converted a JSON file into a data.frame with a a nested list structure, which I would like to unnest and flatten. Some of the values in the list are NULL, which unnest does not accept. If I replace the NULL values with a data.frame structure that has only NA values, I get the desired result. Below is a simplified example of my problem. I have tried to replace the NULL values with the NA data.frame but did not manage because of the the nested structure. How can I achieve the desired result? Example library(tidyr) input1 <- data.frame(id = c("c", "d", "e"), value = c(7, 8, 9)) input2 <- NULL