postgresql-9.4

Distance between two 3D point in geodjango (postgis)

北战南征 提交于 2020-02-25 07:25:07
问题 I have the following problem: I created two points, for example: SRID=3857;POINT Z (62780.8532226825 5415035.177460473 100) SRID=3857;POINT Z (62785.8532226825 5415035.177460473 70) As you can see, there is 5m difference in X coordinates, and 30m in Z coordinates. When I run a.distance(b) in django shell, it returns 5 , which is wrong. However, whenIi run in a psql shell: SELECT ST_3DDistance(a.coordinates, b.coordinates) FROM restapi_entityxyz a, restapi_entityxyz b WHERE a.external_id=

Add condition to sub select in query

帅比萌擦擦* 提交于 2020-02-06 07:52:30
问题 I have this query SELECT DISTINCT u.email, ( SELECT count(DISTINCT o.id) FROM orders o INNER JOIN cart_dates cd ON cd.order_id = o.id WHERE u.id = o.user_id ) as count FROM users u How can I get rows only when count is, for example, < 20? 回答1: you can use group by and having clause. select u.email from users u inner join orders o on o.user_Id = u.id inner join card_dates cd on cd.order_id = o.id group by u.email having count(distinct o.id) < 20 来源: https://stackoverflow.com/questions/59709945

Add condition to sub select in query

巧了我就是萌 提交于 2020-02-06 07:51:15
问题 I have this query SELECT DISTINCT u.email, ( SELECT count(DISTINCT o.id) FROM orders o INNER JOIN cart_dates cd ON cd.order_id = o.id WHERE u.id = o.user_id ) as count FROM users u How can I get rows only when count is, for example, < 20? 回答1: you can use group by and having clause. select u.email from users u inner join orders o on o.user_Id = u.id inner join card_dates cd on cd.order_id = o.id group by u.email having count(distinct o.id) < 20 来源: https://stackoverflow.com/questions/59709945

Add condition to sub select in query

这一生的挚爱 提交于 2020-02-06 07:51:06
问题 I have this query SELECT DISTINCT u.email, ( SELECT count(DISTINCT o.id) FROM orders o INNER JOIN cart_dates cd ON cd.order_id = o.id WHERE u.id = o.user_id ) as count FROM users u How can I get rows only when count is, for example, < 20? 回答1: you can use group by and having clause. select u.email from users u inner join orders o on o.user_Id = u.id inner join card_dates cd on cd.order_id = o.id group by u.email having count(distinct o.id) < 20 来源: https://stackoverflow.com/questions/59709945

Postgres multi-column index (integer, boolean, and array)

↘锁芯ラ 提交于 2020-02-02 15:22:35
问题 I have a Postgres 9.4 database with a table like this: | id | other_id | current | dn_ids | rank | |----|----------|---------|---------------------------------------|------| | 1 | 5 | F | {123,234,345,456,111,222,333,444,555} | 1 | | 2 | 7 | F | {123,100,200,900,800,700,600,400,323} | 2 | (update) I already have a couple indexes defined. Here is the CREATE TABLE syntax: CREATE TABLE mytable ( id integer NOT NULL, other_id integer, rank integer, current boolean DEFAULT false, dn_ids integer[]

Postgres multi-column index (integer, boolean, and array)

感情迁移 提交于 2020-02-02 15:19:26
问题 I have a Postgres 9.4 database with a table like this: | id | other_id | current | dn_ids | rank | |----|----------|---------|---------------------------------------|------| | 1 | 5 | F | {123,234,345,456,111,222,333,444,555} | 1 | | 2 | 7 | F | {123,100,200,900,800,700,600,400,323} | 2 | (update) I already have a couple indexes defined. Here is the CREATE TABLE syntax: CREATE TABLE mytable ( id integer NOT NULL, other_id integer, rank integer, current boolean DEFAULT false, dn_ids integer[]

Query postgres jsonb by value regardless of keys

我的梦境 提交于 2020-01-23 18:27:29
问题 I want to get all records where any value in jsonb field contains a text regardless of key. For example: field contains json {k1: 'hello', k2: 'world'} . I want to get this record by the text 'hello'. I don't care about keys or any json structure, only values. One dirty hack is to cast the field to varchar where jsonb_field::varchar like ... , but this is ugly and it will match keys and {} as well. Another dirty hack looks like this: SELECT * FROM mytable AS m1 WHERE ( SELECT string_agg(value

How do I query a string from JSON based on another string within the JSON in PostgreSQL?

北城余情 提交于 2020-01-16 08:08:08
问题 MyTable.MyField in my PostgreSQL database contains the following (simplified) JSON block: { "base": { "fields": [ { "fieldid": "c12f", "fieldname": "sizes", "choices": [ { "choiceid": "2db3", "size": "small" }, { "choiceid": "241f", "size": "medium" }, { "choiceid": "3f52", "size": "large" } ], "answer": "241f" } ] } } How can I use the value of answer to extract the chosen size from the choices array please (i.e. in this case "medium")? (Note: I have tried. For a TLDR version of this

PostgreSQL Switchover and Switchback in 9.4.1

孤街醉人 提交于 2020-01-15 08:48:23
问题 Environment: PostgreSQL EDB 9.4.1 OS:rhel 7 I have configured streaming replication with continuous archiving. I have performed the steps below for switch-over and switchback. I have read this other articles. I am confused what happens if the archive location is not a shared location. I have followed the steps below for switchover and switchback. SWITCHOVER At the master (192.xxxx.128) pg_ctl -D /opt/PostgresPlus/9.4AS/data stop --mode=fast Create recovery.conf : standby_mode = 'on' primary

Go sql - prepared statement scope

孤人 提交于 2020-01-11 12:21:52
问题 I'm building an API using the Go (1.6.x) sql package along with with PostGres (9.4). Should my prepared statements have application or request scope? Having read the docs it would seem more efficient to scope them at the application level to reduce the number of preparation phases. However, perhaps there are other considerations and prepared statements are not designed to live that long? 回答1: Prepared statements are so that you can execute repetitive SQL commands which may only differ in