postgresql-9.3

PostgreSQL convert month name to number

吃可爱长大的小学妹 提交于 2019-12-12 19:27:17
问题 I have a query that returns a field as month and year for example "Nov 2015" I want to get the number for the month, here I have to get number as "11". 回答1: You can use the to_date function that can parse input in a certain format into a 'real' date. Then with this date you can extract the month from it. select EXTRACT(MONTH FROM to_date('Nov 2015', 'Mon YYYY')) See http://www.postgresql.org/docs/current/static/functions-formatting.html for the formatting syntax and http://www.postgresql.org

Insert record dynamically inside of Procedural Trigger

耗尽温柔 提交于 2019-12-12 15:12:56
问题 We are looking to convert our database over to Postgres (9.3.5), which I have no experience with, and I am trying to get our audit tables up and running. I understand that each table will need its own trigger, but all triggers can call a single function. The trigger on the table is passing a list of the columns that need to be audited since some of our columns are not tracked. Here are some of the posts I followed: - https://stackoverflow.com/a/7915100/229897 - http://www.postgresql.org/docs

Select rows with subtotals maintaining proper order

≯℡__Kan透↙ 提交于 2019-12-12 14:00:07
问题 I am trying to get the output of my query as below please help me partno value value1 phaseid ABCD 10 11 A ABCD 11 12 B Subtotal 21 23 DEFG 20 21 C Subtotal 20 21 IJKL 30 31 A IJKL 31 32 D Subtotal 61 63 CREATE TABLE test_table ( partno text, value bigint, value1 bigint, phaseid text ) INSERT INTO test_table( partno, value, value1, phaseid) VALUES ('ABCD', 10, 11, 'A'),('ABCD', 11, 12, 'B'),('DEFG', 20, 21, 'C'),('IJKL', 30, 31, 'A'), ('IJKL', 31, 321, 'D'); Below is the Query i am using

How to escape underscores in Postgresql

落爺英雄遲暮 提交于 2019-12-12 09:29:18
问题 When searching for underscores in Postgresql, literal use of the character _ doesn't work. For example, if you wanted to search all your tables for any columns that ended in _by , for something like change log or activity information, e.g. updated_by , reviewed_by , etc., the following query almost works: SELECT table_name, column_name FROM information_schema.columns WHERE column_name LIKE '%_by' It basically ignores the underscore completely and returns as if you'd searched for LIKE '%by' .

How to get the discount number of customers in prior period?

故事扮演 提交于 2019-12-12 04:52:03
问题 I have a requirement where I supposed to roll customer data in the prior period of 365 days. Table: CREATE TABLE orders ( persistent_key_str character varying, ord_id character varying(50), ord_submitted_date date, item_sku_id character varying(50), item_extended_actual_price_amt numeric(18,2) ); Sample data: INSERT INTO orders VALUES ('01120736182','ORD6266073' ,'2010-12-08','100856-01',39.90), ('01120736182','ORD33997609' ,'2011-11-23','100265-01',49.99), ('01120736182','ORD33997609' ,'2011

Constraint to avoid combination of foreign keys

我的梦境 提交于 2019-12-12 02:52:16
问题 I've here a problem that I couldn't find a proper solution on my researches, maybe it's because I couldn't find out the exact terms to search for it, so if this is a duplicate I will delete it. My problem is I want to know if it is possible to avoid a combination of data between two fields. I will show the structure and the kind of data I want to avoid. It will be easier to understand. Table_A Table_B ------------------------ ------------------------------- id integer (PK) id integer (PK)

Search in PostgreSQL table

女生的网名这么多〃 提交于 2019-12-12 01:37:35
问题 I have this table in which I would like to implement search filter. CREATE TABLE ACCOUNT( ID INTEGER NOT NULL, USER_NAME TEXT, PASSWD TEXT, FIRST_NAME TEXT, LAST_NAME TEXT, LAST_LOGIN DATE, DATE_REGISTERED DATE, ROLE INTEGER, CAN_LOGIN INTEGER ) ; -- ADD KEYS FOR TABLE ACCOUNT ALTER TABLE ACCOUNT ADD CONSTRAINT KEY1 PRIMARY KEY (ID) ; String searchString = "32"; SELECT * FROM ACCOUNT WHERE " + searchString + " IN (ID, USER_NAME, FIRST_NAME, LAST_NAME) ORDER BY %S %S offset ? limit ?; I get

pgAdmin III Server doesn't listen

北城余情 提交于 2019-12-11 23:49:32
问题 I'm aware that this issue has been asked multiple times, but after searching for hours and trying multiple solutions, nothing has worked for me. Issue: I cannot connect remotely to my postgresql-9.3 server using pgAdmin III. The postgres server is on RHEL6 and pgAdmin GUI is on Windows. pg_hba.conf file: I've just ended up allowing all connections until I figure out the issue. local all all trust # IPv4 local connections: host all all 0.0.0.0/0 trust # IPv6 local connections: host all all ::1

How to insert a created_date and updated_date in JSON object in JSONB column in PostgreSQL

荒凉一梦 提交于 2019-12-11 23:30:02
问题 {"request": 12, "createdby": "sam"} is the JSON data to be inserted in a PostgreSQL table with two columns request id int, data JSONB. The Jain data can be inserted into data column with JSONB datatype. can we append created_date in the JSON object before inserting it into the data column. 回答1: sure, you can just concat jsonb with || operator t=# create table so9(rid int, d jsonb); CREATE TABLE t=# insert into so9 (d) select '{"request": 12, "createdby": "sam"}'::jsonb || concat('{"created

How to find out bad data causing this insert to fail

半腔热情 提交于 2019-12-11 15:18:40
问题 I have a database (Postgres 9.3.5) of 80 millions records, the insert query below fails with: ERROR: invalid input syntax for integer: "" INSERT INTO DISCOGS.TRACK_DURATION SELECT track_id, duration, hours_as_seconds + minutes_as_seconds + seconds as total_seconds FROM ( select track_id, duration, CASE WHEN duration like '%:%:%' THEN (split_part(duration, ':', 1))::bigint * 60 * 60 ELSE 0 END as hours_as_seconds, CASE WHEN duration like '%:%:%' THEN (split_part(duration, ':', 2))::bigint * 60