postgresql-9.3

Query combinations with nested array of records in JSON datatype

无人久伴 提交于 2019-11-27 23:16:39
I'm working on a Rails application that utilizes the Postgres JSON data type. I have a JSON column called data in a table called reports . Let's say I have multiple entries like this: Entry 1: {"objects":[{"album": 1, "src":"fooA.png", "pos": "top"}, {"album": 2, "src":"barB.png", "pos": "top"}], "background":"background.png"} Entry 2: {"objects":[{"album": 1, "src":"fooA.png", "pos": "top"}, {"album": 2, "src":"barC.png", "pos": "top"}], "background":"bacakground.png"} Entry 3: {"objects":[{"album": 1, "src":"fooA.png", "pos": "middle"},{"album": 2, "src":"barB.png", "pos": "middle"}],

postgresql list and order tables by size

自古美人都是妖i 提交于 2019-11-27 19:47:15
问题 Is there an easy way to list all tables from PostgreSQL database and order them by size? Pseudo-code SELECT * FROM tables ORDER by tables.size I am using PostgreSQL 9.3.2 . 回答1: select table_name, pg_relation_size(quote_ident(table_name)) from information_schema.tables where table_schema = 'public' order by 2 This shows you the size of all tables in the schema public if you have multiple schemas, you might want to use: select table_schema, table_name, pg_relation_size('"'||table_schema||'"."'

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

PostgreSQL 9.3: Dynamic pivot table

守給你的承諾、 提交于 2019-11-27 16:30:42
I have a table called as matrix which contains two columns namely cola and colb as shown below: Table : matrix create table matrix ( cola varchar(10), colb varchar(10) ); Insertion of rows : insert into matrix values('A1','B1'),('A2','B2'),('A3','B3'),('A4','B4'), ('A5','B5'),('A6','B6'),('A7','B7'),('A8','B8'), ('A9','B9'),('A10','B10'),('A11','B11'),('A12','B12'), ('A13','B13'),('A14','B14'),('A15','B15'),('A16','B16'), ('A17','B17'),('A18','B18'),('A19','B19'),('A20','B20'), ('A21','B21'),('A22','B22'),('A23','B23'),('A24','B24'), ('A25','B25'),('A26','B26'),('A27','B27'),('A28','B28'), (

Parallel unnest() and sort order in PostgreSQL

本秂侑毒 提交于 2019-11-27 09:18:33
I understand that using SELECT unnest(ARRAY[5,3,9]) as id without an ORDER BY clause, the order of the result set is not guaranteed. I could for example get: id -- 3 5 9 But what about the following request: SELECT unnest(ARRAY[5,3,9]) as id, unnest(ARRAY(select generate_series(1, array_length(ARRAY[5,3,9], 1)))) as idx ORDER BY idx ASC Is it guaranteed that the 2 unnest() calls (which have the same length) will unroll in parallel and that the index idx will indeed match the position of the item in the array? I am using PostgreSQL 9.3.3. Erwin Brandstetter Yes, that is a feature of Postgres

How to create index on json field in Postgres 9.3

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 06:17:34
In PostgreSQL 9.3 Beta 2 (?), how do I create an index on a JSON field? I tried it using the -> operator used for hstore but got the following error: CREATE TABLE publishers(id INT, info JSON); CREATE INDEX ON publishers((info->'name')); ERROR: data type json has no default operator class for access method "btree" HINT: You must specify an operator class for the index or define a default operator class for the data type. rlib Found: CREATE TABLE publishers(id INT, info JSON); CREATE INDEX ON publishers((info->>'name')); 来源: https://stackoverflow.com/questions/17807030/how-to-create-index-on

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;

Import psycopg2 Library not loaded: libssl.1.0.0.dylib

帅比萌擦擦* 提交于 2019-11-27 00:03:14
When I try to run the command: import psycopg2 I get the error: ImportError: dlopen(/Users/gwulfs/anaconda/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: libssl.1.0.0.dylib Referenced from: /Users/gwulfs/anaconda/lib/python2.7/site-packages/psycopg2/_psycopg.so Reason: image not found So far I have tried brew install openssl and have referenced (with no luck): psycopg2 installation error - Library not loaded: libssl.dylib http://joshuakehn.com/2013/10/13/Postgresapp-and-psycopg2-on-OS-X.html Psycopg2 image not found Instead of playing with symlinks in system library

How to count days except Sundays between two dates in Postgres?

三世轮回 提交于 2019-11-26 23:32:27
问题 To find the number of days between two dates we can use something like this: SELECT date_part('day',age('2017-01-31','2017-01-01')) as total_days; In the above query we got 30 as output instead of 31. Why is that? And I also want to find the number of days except Sundays. Expected output for the interval ('2017-01-01', '2017-01-31') : Total Days = 31 Total Days except Sundays = 26 回答1: You need to define "between two dates" more closely. Lower and upper bound included or excluded? A common

PostgreSQL 9.3: Dynamic pivot table for huge records

ぃ、小莉子 提交于 2019-11-26 22:27:46
问题 I want to display a pivot table for the following data. I have a table with the 10000 records. Table : Mat_test CREATE TABLE Mat_test AS SELECT DISTINCT 1 + floor(random() * 1500000)::integer AS id, 2 + floor(random() * 1600000)::integer AS Numbers FROM generate_series(1, 10000) g; Now creating function from which I want to display the crosstab table for the above records: Function : Mat_test_cross_tab CREATE OR REPLACE FUNCTION Mat_test_cross_tab (_retType anyelement , table_name varchar)