lateral

JOIN on set returning function results

蹲街弑〆低调 提交于 2019-12-09 13:27:52
问题 I am trying to join table and function which returns rows: SELECT p.id, p.name, f.action, f.amount FROM person p JOIN calculate_payments(p.id) f(id, action, amount) ON (f.id = p.id); This function returns 0, 1 or more rows for each id. The query works on PostgreSQL 9.3, but on 9.1 it shows following error: ERROR: invalid reference to FROM-clause entry for table "p" HINT: There is an entry for table "p", but it cannot be referenced from this part of the query I cannot move out calculations

Hive 中的复合数据结构简介以及一些函数的用法说明

↘锁芯ラ 提交于 2019-12-03 04:02:50
目前 hive 支持的复合数据类型有以下几种: map (key1, value1, key2, value2, ...) Creates a map with the given key/value pairs struct (val1, val2, val3, ...) Creates a struct with the given field values. Struct field names will be col1, col2, ... named_struct (name1, val1, name2, val2, ...) Creates a struct with the given field names and values. (as of Hive 0.8.0) array (val1, val2, ...) Creates an array with the given elements create_union (tag, val1, val2, ...) Creates a union type with the value that is being pointed to by the tag parameter 一、map、struct、array 这3种的用法: 1、Array的使用 创建数据库表,以array作为数据类型 create table

Dynamically execute query using the output of another query

こ雲淡風輕ζ 提交于 2019-12-02 02:02:41
I have a function called generate_table, that takes 2 input parameters ( rundate::date and branch::varchar ) Now I am trying to work on a second function, using PLPGSQL, that will get a list of all branches and the newest date for each branch and pass this as a parameter to the generate_table function. The query that I have is this: select max(rundate) as rundate, branch from t_index_of_imported_files group by branch and it results on this: rundate;branch 2014-03-13;branch1 2014-03-12;branch2 2014-03-10;branch3 2014-03-13;branch4 and what I need is that the function run something like this

Convert one row into multiple rows with fewer columns

北战南征 提交于 2019-11-28 14:22:01
I'd like to convert single rows into multiple rows in PostgreSQL, where some of the columns are removed. Here's an example of the current output: name | st | ot | dt | -----|----|----|----| Fred | 8 | 2 | 3 | Jane | 8 | 1 | 0 | Samm | 8 | 0 | 6 | Alex | 8 | 0 | 0 | Using the following query: SELECT name, st, ot, dt FROM times; And here's what I want: name | t | val | -----|----|-----| Fred | st | 8 | Fred | ot | 2 | Fred | dt | 3 | Jane | st | 8 | Jane | ot | 1 | Samm | st | 8 | Samm | dt | 6 | Alex | st | 8 | How can I modify the query to get the above desired output? SELECT times.name, x.t,

Find most common elements in array with a group by

情到浓时终转凉″ 提交于 2019-11-28 12:47:06
I have a table of rows with the following structure name TEXT, favorite_colors TEXT[], group_name INTEGER where each row has a list of everyone's favorite colors and the group that person belongs to. How can I GROUP BY group_name and return a list of the most common colors in each group? Could you do a combination of int[] && int[] to set for overlap, int[] & int[] to get the intersection and then something else to count and rank? Quick and dirty: SELECT group_name, color, count(*) AS ct FROM ( SELECT group_name, unnest(favorite_colors) AS color FROM tbl ) sub GROUP BY 1,2 ORDER BY 1,3 DESC;

Find most common elements in array with a group by

天大地大妈咪最大 提交于 2019-11-27 19:24:53
问题 I have a table of rows with the following structure name TEXT, favorite_colors TEXT[], group_name INTEGER where each row has a list of everyone's favorite colors and the group that person belongs to. How can I GROUP BY group_name and return a list of the most common colors in each group? Could you do a combination of int[] && int[] to set for overlap, int[] & int[] to get the intersection and then something else to count and rank? 回答1: Quick and dirty: SELECT group_name, color, count(*) AS ct

GROUP BY in Postgres - no equality for JSON data type?

孤者浪人 提交于 2019-11-27 06:37:08
问题 I have the following data in a matches table: 5;{"Id":1,"Teams":[{"Name":"TeamA","Players":[{"Name":"AAA"},{"Name":"BBB"}]},{"Name":"TeamB","Players":[{"Name":"CCC"},{"Name":"DDD"}]}],"TeamRank":[1,2]} 6;{"Id":2,"Teams":[{"Name":"TeamA","Players":[{"Name":"CCC"},{"Name":"BBB"}]},{"Name":"TeamB","Players":[{"Name":"AAA"},{"Name":"DDD"}]}],"TeamRank":[1,2]} I want to select each last distinct Team in the table by their name. i.e. I want a query that will return: 6;{"Name":"TeamA","Players":[{

Query for element of array in JSON column

。_饼干妹妹 提交于 2019-11-27 01:01:11
Recently upgraded to using PostgreSQL 9.3.1 to leverage the JSONfunctionalities. In my table I have a json type column that has a structure like this: { "id": "123", "name": "foo", "emails":[ { "id": "123", "address": "somethinghere" }, { "id": "456", "address": "soemthing" } ] } This is just dummy data for the purpose of the question. Is it possible to query for a specific item in the emails array based on the id? Pretty much: "return email where id=123)"? Erwin Brandstetter Yes, that's possible: SELECT * FROM tbl t, json_array_elements(t.json_col->'emails') AS elem WHERE elem->>'id' = 123;

Postgres analogue to CROSS APPLY in SQL Server

空扰寡人 提交于 2019-11-26 21:01:43
I need to migrate SQL queries written for MS SQL Server 2005 to Postgres 9.1. What is the best way to substitute for CROSS APPLY in this query? SELECT * FROM V_CitizenVersions CROSS APPLY dbo.GetCitizenRecModified(Citizen, LastName, FirstName, MiddleName, BirthYear, BirthMonth, BirthDay, ..... ) -- lots of params GetCitizenRecModified() function is a table valued function. I can't place code of this function because it's really enormous, it makes some difficult computations and I can't abandon it. Erwin Brandstetter In Postgres 9.3 or later use a LATERAL join: SELECT v.col_a, v.col_b, f.* --

Postgres analogue to CROSS APPLY in SQL Server

China☆狼群 提交于 2019-11-26 09:03:37
问题 I need to migrate SQL queries written for MS SQL Server 2005 to Postgres 9.1. What is the best way to substitute for CROSS APPLY in this query? SELECT * FROM V_CitizenVersions CROSS APPLY dbo.GetCitizenRecModified(Citizen, LastName, FirstName, MiddleName, BirthYear, BirthMonth, BirthDay, ..... ) -- lots of params GetCitizenRecModified() function is a table valued function. I can\'t place code of this function because it\'s really enormous, it makes some difficult computations and I can\'t