postgresql-9.1

Postgresql: Ordering columns to match custom criteria

风流意气都作罢 提交于 2019-12-06 11:55:41
I'm having trouble sorting the result of a query. After executing this query: SELECT id_doc_header, id_clasificacion_doc FROM cabecera_documento INNER JOIN tipo_doc USING (id_tipo_doc) INNER JOIN clasificacion_documento USING (id_clasificacion_doc) WHERE finalizado = 'f' AND cod_exp = '10-APC-2013' AND id_clasificacion_doc in(2,3,4,5) ORDER BY case when Id_clasificacion_doc = 5 THEN 5 when Id_clasificacion_doc = 3 THEN 3 when Id_clasificacion_doc = 2 THEN 2 when Id_clasificacion_doc = 4 THEN 4 END; or this: SELECT id_doc_header, id_clasificacion_doc FROM cabecera_documento INNER JOIN tipo_doc

Variables for identifiers inside IF EXISTS in a plpgsql function

落爺英雄遲暮 提交于 2019-12-06 11:28:06
CREATE OR REPLACE FUNCTION drop_now() RETURNS void AS $BODY$ DECLARE row record; BEGIN RAISE INFO 'in'; FOR row IN select relname from pg_stat_user_tables WHERE schemaname='public' AND relname LIKE '%test%' LOOP IF EXISTS(SELECT row.relname.tm FROM row.relname WHERE row.relname.tm < current_timestamp - INTERVAL '90 minutes' LIMIT 1) THEN -- EXECUTE 'DROP TABLE ' || quote_ident(row.relname); RAISE INFO 'Dropped table: %', quote_ident(row.relname); END IF; END LOOP; END; $BODY$ LANGUAGE plpgsql VOLATILE; Could you tell me how to use variables in SELECT which is inside IF EXISTS ? At the present

Not able to install pg gem on Ubuntu 12.10

给你一囗甜甜゛ 提交于 2019-12-06 10:28:43
I am using Ubuntu 12.10 64-bit and I have following packages installed : dpkg --get-selections | grep postgre output: postgresql postgresql-9.1 postgresql-client postgresql-client-9.1 postgresql-client-common postgresql-common postgresql-contrib postgresql-contrib-9.1 postgresql-server-dev-9.1 postgresql-server-dev-all libpq-dev libpq5 rvm 1.17.9 ruby 1.9.3p362 (2012-12-25 revision 38607) [x86_64-linux] I am not able to install pg gem I am getting following error : ERROR: Error installing pg: ERROR: Failed to build gem native extension. /home/vedarthk/.rvm/rubies/ruby-1.9.3-p362/bin/ruby

Retrieving file from bytea in PostgreSQL using java

时光毁灭记忆、已成空白 提交于 2019-12-06 07:39:09
问题 Hi I'm using the below code to retrieve the file from the postgresql bytea using java, but inside the file I'm getting numbers like 314530413142313141 File file = new File("c:/test.doc"); FileOutputStream fos = new FileOutputStream(file); ResultSet rs = st.executeQuery("SELECT * FROM test_bytea where id=" + 1); if (rs != null) { while (rs.next()) { byte[] fileBytes = new byte[1024]; InputStream is = rs.getBinaryStream("type_file"); while (is.read(fileBytes) > 0) { fos.write(fileBytes); } //

Postgres SELECT* FROM table WHERE column-varchar==“string-example”?

你。 提交于 2019-12-06 07:25:40
问题 I have the following table: CREATE TABLE lawyer ( id SERIAL PRIMARY KEY, name VARCHAR NOT NULL UNIQUE, name_url VARCHAR check(translate(name_url, 'abcdefghijklmnopqrstuvwxyz-', '') = '') NOT NULL UNIQUE ); I want to SELECT * FROM lawyer where name_url = "john-doe" 回答1: Character literals are put into single quotes: SELECT * FROM lawyer where name_url = 'john-doe'; See the manual for details: https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS 回答2:

Return records that don't have specific key - Rails4 HStore Postgresql query

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 05:20:39
I have a model Task t.string "name" t.hstore "actions" Example: #<Task id: 1, name: "first", actions: {"today"=>"9"}, #<Task id: 2, name: "second", actions: {"yesterday"=>"1"}, #<Task id: 3, name: "third", actions: nil, #<Task id: 4, name: "four", actions: {"today"=>"11"}, I need to find all records where actions: nil, :today<10 and key :today not present. It is 1,2,3 task. Task.where("actions -> 'today' < '10'") - it return only first task. Task.where("actions -> 'today' < '10' OR actions IS NULL") - it return 1 and 3 records. How i can find all records which do not have the key :today in

Find cluster given node in PostgreSQL

霸气de小男生 提交于 2019-12-06 05:12:05
I am representing a graph in Postgres 9.1 (happens to be bidirectional and cyclic): CREATE TABLE nodes ( id SERIAL PRIMARY KEY, name text ); CREATE TABLE edges ( id SERIAL PRIMARY KEY, node1_id int REFERENCES nodes(id), node2_id int REFERENCES nodes(id) ); Given a particular node ID, want to retrieve all other nodes in that cluster. I started with the "Paths from a single node" example here , and this is where I got: WITH RECURSIVE search_graph(id, path) AS ( SELECT id, ARRAY[id] FROM nodes UNION SELECT e.node2_id, sg.path || e.node2_id FROM search_graph sg JOIN edges e ON e.node1_id = sg.id )

PostgreSQL Composite Primary Key and Serial increment?

爷,独闯天下 提交于 2019-12-06 03:35:53
I'm trying to create a table as follows: CREATE TABLE SCHEDULE ( SESSIONID SERIAL, MODULECODE VARCHAR(10), CONSTRAINT SCHEDULE_FOREIGN_KEY FOREIGN KEY (MODULECODE) REFERENCES MODULES (MODULECODE), CONSTRAINT SCHEDULE_PRIMARY_KEY PRIMARY KEY (SESSIONID, MODULECODE)); The idea being that SESSION ID would auto increment with each new row but only local to MODULECODE , for example: ---------------------- |SESSIONID|MODULECODE| |---------|----------| | 1 | A | | 2 | A | | 3 | A | | 1 | B | | 2 | B | | 1 | C | | 2 | C | |--------------------| I believe this is how AUTO_INCREMENT functions in MySQL

Changing ORDER BY from id to another indexed column (with low LIMIT) has a huge cost

落爺英雄遲暮 提交于 2019-12-06 02:38:37
I have a query on a 500 000 row table. Basically WHERE s3_.id = 287 ORDER BY m0_.id DESC LIMIT 25 => Query runtime = 20ms WHERE s3_.id = 287 ORDER BY m0_.created_at DESC LIMIT 25 => Query runtime = 15000ms or more There is an index on created_at. The Query plans are completely different. Unfortunately I am not a query plan guru. I would like to reproduce the fast query plan while ordering by created_at. Is that possible and how would I go about that ? Query Plan - Slow Query (order by m0_.created_at) : http://explain.depesz.com/s/KBl Query Plan - Fast Query ( order by m0_.id ): http://explain

Create array in SELECT

好久不见. 提交于 2019-12-06 01:31:14
问题 I'm using PostgreSQL 9.1 and I have this data structure: A B ------- 1 a 1 a 1 b 1 c 1 c 1 c 1 d 2 e 2 e I need a query that produces this result: 1 4 {{c,3},{a,2},{b,1},{d,1}} 2 1 {{e,2}} A=1, 4 rows total with A=1, the partial counts (3 rows with c value, 2 rows with a value, .....) The distinct values of column "A" The count of all rows related to the "A" value An array contains all the elements related to the "A" value and the relative count of itself The sort needed for the array is