postgresql

Change column order in table of postgres

霸气de小男生 提交于 2021-02-05 09:38:22
问题 I am trying to change the column order in the table of postgressql. But I didn't get any clue or answer. I think this functionality is add in new version. I am using postgres 11. Ex No column 1 Id 2 Lastname 3 Firstname Now i want to change Firstname on 2nd position. 回答1: You would have to drop and re-create the table or at least the lastname column for that: BEGIN; ALTER TABLE atable RENAME lastname TO oldcol; ALTER TABLE atable ADD lastname text NOT NULL; UPDATE atable SET lastname = oldcol

Change column order in table of postgres

倖福魔咒の 提交于 2021-02-05 09:37:18
问题 I am trying to change the column order in the table of postgressql. But I didn't get any clue or answer. I think this functionality is add in new version. I am using postgres 11. Ex No column 1 Id 2 Lastname 3 Firstname Now i want to change Firstname on 2nd position. 回答1: You would have to drop and re-create the table or at least the lastname column for that: BEGIN; ALTER TABLE atable RENAME lastname TO oldcol; ALTER TABLE atable ADD lastname text NOT NULL; UPDATE atable SET lastname = oldcol

UPDATE table_name SET col_name = varchar WHERE col_name is NULL;

雨燕双飞 提交于 2021-02-05 09:29:19
问题 The following UPDATE fails :- UPDATE table_name SET col_name = varchar WHERE col_name is NULL; The failure message is :- ERROR: column "varchar" does not exist Whereas the undermentioned one succeeds :- UPDATE table_name SET col_name = 889977 WHERE col_name is NULL; I have checked the pg_typeof of the column - col_name is character varying . Kindly help. 回答1: i think you missed quote for string UPDATE table_name SET col_name = 'varchar' WHERE col_name is NULL; 来源: https://stackoverflow.com

Combining 3 SELECT statements to output 1 table

戏子无情 提交于 2021-02-05 08:51:38
问题 I have three queries with results. Query 1: SELECT DISTINCT employeeid, work.clientid, ROUND ((CAST (AVG(current_lawn_price) AS numeric) / CAST (AVG((((EXTRACT(HOUR FROM job_finish)*60) + EXTRACT(MIN FROM job_finish))) - ((EXTRACT(HOUR FROM job_start)*60) + EXTRACT(MIN FROM job_start))) AS numeric)) / 1, 2) AS under_over_1 FROM work JOIN timesheet USING (date_linkid) JOIN client USING (clientid) WHERE employeeid = 1 AND workid < 557 AND workid > 188 GROUP BY employeeid, clientid ORDER BY

Using function output in SQLAlchemy join clause

[亡魂溺海] 提交于 2021-02-05 08:25:15
问题 I am trying to translate a fairly short bit of SQL into an sqlAlchemy ORM query. The SQL uses Postgres's generate_series to make a set of dates and my goal is to make a set of time series arrays categorized by one of the columns. The tables (simplified) are very simple: counts: ----------------- count (Integer) day (Date) placeID (foreign key related to places) "counts_pkey" PRIMARY KEY (day, placeID) places: ----------------- id name (varchar) The output I'm after is a time series of counts

how to split a string in a column field value of a table to multiple rows in select query in postgresql

荒凉一梦 提交于 2021-02-05 07:54:09
问题 table1 ID ITEM ----------------------- 1 a|b|c 2 x|y|z 3 Q||S 4 |||| 5 J|K|Y|U I want to insert above ITEM to table2 as following manner table2 SLNO ID ITEMS ------------------------ 1 1 a 2 1 b 3 1 c 4 2 x 5 2 y 6 2 z 7 3 Q 8 3 S 9 5 J 10 5 K 11 5 Y 12 5 U so i have used INSERT INTO table2("ID","ITEMS")SELECT ID,split_part("ITEM", '|',1) AS ITEMS FROM table1 the problem is split_part() need to specify a postion index,so it only split and shows first positions charaters means, ID ITEMS ------

Can I have a postgres plpgsql function return variable-column records?

喜欢而已 提交于 2021-02-05 07:52:05
问题 I want to create a postgres function that builds the set of columns it returns on-the-fly; in short, it should take in a list of keys, build one column per-key, and return a record consisting of whatever that set of columns was. Briefly, here's the code: CREATE OR REPLACE FUNCTION reports.get_activities_for_report() RETURNS int[] AS $F$ BEGIN RETURN ARRAY(SELECT activity_id FROM public.activity WHERE activity_id NOT IN (1, 2)); END; $F$ LANGUAGE plpgsql STABLE; CREATE OR REPLACE FUNCTION

Postgres unique combination constraint across tables

陌路散爱 提交于 2021-02-05 07:45:26
问题 I have three tables - file ( file_id int primary key filename text not null etc... ) product ( product_id int primary key etc.... ) product_attachment ( product_id references product file_id references file ) I want to ensure that when these are natural-joined, product_id + filename is unique. The best solution I have so far involves adding filename to the product_attachment table, but I'm wondering if there's a way to avoid that. 回答1: If the filename column is not unique you can add a custom

In PHP PDO how to get “RETURNING” clause values of PostgreSQL upsert query

时光总嘲笑我的痴心妄想 提交于 2021-02-05 07:22:05
问题 I have this upsert query written in postgreSQL $statement = 'INSERT INTO "CharactersUnlockToBuyLevels" ("CharacterId", "LevelId", "AmountToBuy", "EagleStatueId", "Location", "MapCoordinateTop", "MapCoordinateLeft") VALUES (:CharacterId, :LevelId, :AmountToBuy, :EagleStatueId, :Location, :MapCoordinateTop, :MapCoordinateLeft) ON CONFLICT ("CharacterId") DO UPDATE SET "LevelId" = EXCLUDED."LevelId", "AmountToBuy" = EXCLUDED."AmountToBuy", "EagleStatueId" = EXCLUDED."EagleStatueId", "Location" =

“x is a procedure, use ”call“” when I am already using call

拈花ヽ惹草 提交于 2021-02-05 07:09:49
问题 I'm using Postgres 12 and have written this procedure: CREATE OR REPLACE PROCEDURE reduceStock(id INTEGER, soldQuantity INTEGER) LANGUAGE plpgsql AS $$ BEGIN UPDATE inventory SET ProductStockAmount = ProductStockAmount - soldQuantity WHERE ProductID = id; END; $$; It works perfectly if I open up psql on the command line and run call reduceStock(1,1); However, calling it from my Java program as follows: CallableStatement stmt = conn.prepareCall("{call reduceStock(?, ?)}"); stmt.setInt(1,