postgresql

What is the default select order in PostgreSQL or MySQL?

穿精又带淫゛_ 提交于 2021-01-28 00:34:54
问题 I have read in the PostgreSQL docs that without an ORDER statement, SELECT will return records in an unspecified order. Recently on an interview, I was asked how to SELECT records in the order that they inserted without an PK or created_at or other field that can be used for order. The senior dev who interviewed me was insistent that without an ORDER statement the records will be returned in the order that they were inserted. Is this true for PostgreSQL? Is it true for MySQL? Or any other

fill column with last value from partition in postgresql

喜你入骨 提交于 2021-01-28 00:26:47
问题 I'm trying to return the last value of a partition and apply it to the rest of the column For example, if I have the below... ID Date Status 1 20150101 1 20150201 1 20150301 1 20150401 void 2 20150101 2 20150201 2 20150301 I want to return this. ID Date Status 1 20150101 void 1 20150201 void 1 20150301 void 1 20150401 void 2 20150101 2 20150201 2 20150301 I've been playing around with the below and similar to no avail. select ID, date, case when status is null then last_value(status ignore

pg_config - how to change postgres versions

自闭症网瘾萝莉.ら 提交于 2021-01-28 00:15:15
问题 By default I had postgres 10 versions on my two ubuntu 14 machines, but I installed postgres 9.6 on the two of them. But on one of them pg_config give me: VERSION = PostgreSQL 9.6.6 And the other: VERSION = PostgreSQL 10.1 They have the same libpq-dev packages in: dpkg -l | grep libpq-dev libpq-dev 10.1-1.pgdg14.04+1 amd64 header files for libpq5 (PostgreSQL library) So I was wondering is there a way for me to change the version installed that it gives me postgres 9.6 instead of postgres 10

Postgresql 9.4, Make existing primary key as SERIAL

最后都变了- 提交于 2021-01-28 00:13:50
问题 I am using postgresql 9.4. I want to change existing primary key with serial. My query is not working. Anybody know how to do this? Alter table 'table_name' alter column id BIGSERIAL; There should be a single query to modify a particular column. I didn't see that 回答1: CREATE SEQUENCE table_name_id_seq OWNED BY table_name.id; ALTER TABLE table_name ALTER id SET DEFAULT nextval('table_name_id_seq'::regclass); 来源: https://stackoverflow.com/questions/41847396/postgresql-9-4-make-existing-primary

Fetch posts from non blocking users

守給你的承諾、 提交于 2021-01-27 23:42:32
问题 I'm trying to fetch some posts from users that isn't blocking "me". Se models below: User id username .... Post id user_id content ... Blockings blocker_id blocked_id I need to fetch posts from all users that isn't blocking me. I fetch all posts with: @posts Post.all But how do I joins this together. Pseudo SELECT * FROM posts WHERE "posts.user_id isn't blocking me" I have a helper called current_user that returns the current logged in user "me". 回答1: A way to do it with SQL would be: select

Error: cannot insert multiple commands into a prepared statement

一个人想着一个人 提交于 2021-01-27 23:32:47
问题 I am trying to move a row from one table to another. The problem is that if I put both queries together, I get "error: cannot insert multiple commands into a prepared statement". What can I do? exports.deletePost = function(id) { return db.query(`INSERT INTO deletedjobs SELECT * FROM jobs WHERE id = $1; DELETE FROM jobs WHERE id = $1;`, [id]).then(results => { console.log("succesfull transfer"); return results.rows[0]; }); }; 回答1: Maybe you should not use AND clause, try semicolon ; . You

Select 5 of each distinct value

蓝咒 提交于 2021-01-27 23:11:29
问题 I have the following table in PostgreSQL: | a | b | c | =================== | 'w' | 2 | 3 | | 'w' | 7 | 2 | | 'w' | 8 | 1 | | 'w' | 3 | 6 | | 'w' | 0 | 8 | | 'w' | 2 | 9 | | 'w' | 2 | 9 | | 'z' | 4 | 9 | | 'z' | 0 | 9 | | 'z' | 0 | 8 | | 'z' | 3 | 6 | | 'z' | 2 | 7 | | 'z' | 3 | 1 | | 'z' | 3 | 2 | | 'z' | 3 | 3 | I want to select all records, but limit them to 5 records for each distinct value in column a . So the result would look like: | a | b | c | =================== | 'w' | 2 | 3 | | 'w

Count and inner join

[亡魂溺海] 提交于 2021-01-27 22:41:36
问题 Look at this sql request: select distinct erp.users.id from erp.users inner join prod.referral_order_delivered on erp.users.id= prod.referral_order_delivered.user_id::uuid inner join erp.orders on erp.orders."userId"::uuid= erp.users.id where "paidAt"::date >= '2016-06-07' and "paidAt"::date <= '2017-07-07' Let’s say I get a result like this one: id 2 1 4 5 Now I wanna count how many times the value of these ids appear as value of the column userId in the table erp.orders For example, if I

How to implement Oracle count(distinct) over partition in Postgres

大兔子大兔子 提交于 2021-01-27 22:38:27
问题 Here is my sql sample in a select statement. Converting from oracle to postgres, need a simple way to re implement oracle count distinct over partition in postgres. , count(distinct on pm.mobseg_state) over (partition by pm.trans_id) as mob_segments_count , count(distinct on pm.country_reg_region_cd) over (partition by pm.trans_id) as countries_count 回答1: Postgres doesn't support count(distinct) directly. But you can implement it with a subquery: select . . ., sum( (seqnum_tm = 1)::int) as

How to add a request timeout in Typeorm/Typescript?

末鹿安然 提交于 2021-01-27 21:53:52
问题 Today, the behavior of Typeorm (Postgres) for getManager().query(...) and getRepositoty().createQueryBuilder(...).getMany() is to wait for a response indefinitely. Is there a way to introduce a request timeout that I might've missed? If this is not possible, does Typeorm expose the connection from its pool so that I can implement a timeout mechanism and close the DB connection manually? 回答1: To work with a specific connection from the pool use createQueryRunner there is no info about it in