postgresql-9.5

PostgreSql : Json Array to Rows using Lateral Join

坚强是说给别人听的谎言 提交于 2020-05-21 07:34:48
问题 I have two following JSON Array in details field of my table and need to evaluate the query as I use in another relational table. { "city": "London", "name": "Sainburry", "quantities": [112, 145, 222, 122, 124], "prices": [4, 4, 4, 0, 3], "dates": ["13.05.2020", "14.05.2020", "15.05.2020", "16.05.2020", "17.05.2020"] } I want to evaluate the following query for this JSON Array: select quantities, prices, AVG(quantities/prices::float) as ratio from my_table where city = 'London' group by

psycopg2 ImportError: undefined symbol: PQconninfo

筅森魡賤 提交于 2020-05-15 04:58:48
问题 Couldn't able to import psycopg2 Output in python console: import psycopg2 Traceback (most recent call last): File "", line 1, in File "/home/user/.py_virtualenvs/verb_py3/lib/python3.5/site-packages/psycopg2/ init .py", line 50, in from psycopg2._psycopg import ( # noqa ImportError: /home/user/.py_virtualenvs/verb_py3/lib/python3.5/site-packages/psycopg2/_psycopg.cpython-35m-x86_64-linux-gnu.so: undefined symbol: PQconninfo 回答1: Installed the psycopg2-binary package solved my issue. pip

Postgres 9.5 ON CONFLICT DO SELECT

强颜欢笑 提交于 2020-01-21 09:44:17
问题 While doing UPSERT in Postgres 9.5, is it possible to return null on INSERT success and return something ON CONFLICT? I would like to something like this: insert into "user" (timestamp, user_id, member_id) values ($1, $2, $3) ON CONFLICT (user_id, member_id) DO select id from "user" where user_id = $2 returning user_id The only purpose of selecting user_id is to return something (anything other than null) ON CONFLICT, and without writing to disk. I know that this can be done with ON CONFLICT

Postgres 9.5 ON CONFLICT DO SELECT

我是研究僧i 提交于 2020-01-21 09:43:25
问题 While doing UPSERT in Postgres 9.5, is it possible to return null on INSERT success and return something ON CONFLICT? I would like to something like this: insert into "user" (timestamp, user_id, member_id) values ($1, $2, $3) ON CONFLICT (user_id, member_id) DO select id from "user" where user_id = $2 returning user_id The only purpose of selecting user_id is to return something (anything other than null) ON CONFLICT, and without writing to disk. I know that this can be done with ON CONFLICT

Can INSERT […] ON CONFLICT be used for foreign key violations?

为君一笑 提交于 2020-01-19 06:08:38
问题 Given => select * from referenced; referenced_id | name ---------------+------- 1 | one 2 | two 3 | three and => select * from entries; entry_id | referenced_id | name ----------+---------------+------------------ 1 | 3 | references three where referenced_id and entry_id are primary keys. I want an insert statement for entries that skips insertion if either the entry_id already exists or the referenced item does not exist. The first is easily done: INSERT INTO entries VALUES (1, 2,

Can INSERT […] ON CONFLICT be used for foreign key violations?

雨燕双飞 提交于 2020-01-19 06:05:16
问题 Given => select * from referenced; referenced_id | name ---------------+------- 1 | one 2 | two 3 | three and => select * from entries; entry_id | referenced_id | name ----------+---------------+------------------ 1 | 3 | references three where referenced_id and entry_id are primary keys. I want an insert statement for entries that skips insertion if either the entry_id already exists or the referenced item does not exist. The first is easily done: INSERT INTO entries VALUES (1, 2,

Can INSERT […] ON CONFLICT be used for foreign key violations?

杀马特。学长 韩版系。学妹 提交于 2020-01-19 06:04:46
问题 Given => select * from referenced; referenced_id | name ---------------+------- 1 | one 2 | two 3 | three and => select * from entries; entry_id | referenced_id | name ----------+---------------+------------------ 1 | 3 | references three where referenced_id and entry_id are primary keys. I want an insert statement for entries that skips insertion if either the entry_id already exists or the referenced item does not exist. The first is easily done: INSERT INTO entries VALUES (1, 2,

Percentile calculation with a window function

瘦欲@ 提交于 2020-01-15 08:33:45
问题 I know you can get the average, total, min, and max over a subset of the data using a window function. But is it possible to get, say, the median, or the 25th percentile instead of the average with the window function? Put another way, how do I rewrite this to get the id and the 25th or 50th percentile sales numbers within each district rather than the average? SELECT id, avg(sales) OVER (PARTITION BY district) AS district_average FROM t 回答1: You can write this as an aggregation function

how to get the table row count of all the tables present in particular schema in postgresql 9.5?

别说谁变了你拦得住时间么 提交于 2020-01-15 02:12:27
问题 how to get the table row count of all the tables present in particular schema in postgresql 9.5? I would like to have the result as table_name | row_count. How this can be done using query? 回答1: This can be done with some XML magic: select table_schema, table_name, (xpath('/row/count/text()', query_to_xml('select count(*) from '||format('%I.%I', table_schema, table_name), true, true, '')))[1]::text::int as row_count from information_schema.tables where table_schema = 'public' 回答2: https://www

how to get the table row count of all the tables present in particular schema in postgresql 9.5?

*爱你&永不变心* 提交于 2020-01-15 02:12:02
问题 how to get the table row count of all the tables present in particular schema in postgresql 9.5? I would like to have the result as table_name | row_count. How this can be done using query? 回答1: This can be done with some XML magic: select table_schema, table_name, (xpath('/row/count/text()', query_to_xml('select count(*) from '||format('%I.%I', table_schema, table_name), true, true, '')))[1]::text::int as row_count from information_schema.tables where table_schema = 'public' 回答2: https://www