postgresql-9.3

Calculating the Weighted Average Cost of products stock

家住魔仙堡 提交于 2019-11-30 04:05:02
I have to calculate my products stock cost, so for every product after each buy, i have to recalculate the Weighted Average Cost . I got a view thats bring me the current product's stock after each in/out: document_type document_date product_id qty_out qty_in price row_num stock_balance SI 01/01/2014 52 0 600 1037.28 1 600 SI 01/01/2014 53 0 300 1357.38 2 300 LC 03/02/2014 53 100 0 1354.16 3 200 LC 03/02/2014 53 150 0 1355.25 4 50 LC 03/02/2014 52 100 0 1035.26 5 500 LC 03/02/2014 52 200 0 1035.04 6 300 LF 03/02/2014 53 0 1040 1356.44 7 1090 LF 03/02/2014 52 0 1560 1045 8 1860 LC 04/02/2014 52

Dynamically generate columns in PostgreSQL

坚强是说给别人听的谎言 提交于 2019-11-30 04:04:53
问题 I have seen that there are quit a few similar questions like this one, but I havent understood how to code it myself. Please have in mind that I am just a beginner in this field. Basically I want to pivot the table like this: zoom | day | point zoom | 2015-10-01 | 2015-10-02 | ...... ------+-----------+------- ---> ------+------------+-------------+ 1 | 2015-10-01 | 201 1 | 201 | 685 | 2 | 2015-10-01 | 43 2 | 43 | 346 | 3 | 2015-10-01 | 80 3 | 80 | 534 | 4 | 2015-10-01 | 324 4 | 324 | 786 | 5

Postgres how to implement calculated column with clause

会有一股神秘感。 提交于 2019-11-30 03:57:16
问题 I need to filter by calculated column in postgres. It's easy with MySQL but how to implement with Postgres SQL ? pseudocode: select id, (cos(id) + cos(id)) as op from myTable WHERE op > 1; Any SQL tricks ? 回答1: If you don't want to repeat the expression, you can use a derived table: select * from ( select id, cos(id) + cos(id) as op from myTable ) as t WHERE op > 1; This won't have any impact on the performance, it is merely syntactic sugar required by the SQL standard. Alternatively you

postgresql sequence nextval in schema

社会主义新天地 提交于 2019-11-30 02:56:59
I have a sequence on postgresql 9.3 inside a schema. I can do this: SELECT last_value, increment_by from foo."SQ_ID"; last_value | increment_by ------------+-------------- 1 | 1 (1 fila) but this not Works: SELECT nextval('foo.SQ_ID'); ERROR: no existe la relación «foo.sq_id» LÍNEA 1: SELECT nextval('foo.SQ_ID'); What is wrong ? It says that not exist the relation «foo.sq_id», but it exists. The quoting rules are painful. I think you want: SELECT nextval('foo."SQ_ID"'); to prevent case-folding of SQ_ID . SELECT last_value, increment_by from "other_schema".id_seq; for adding a seq to a column

pg_config executable not found when using pgxnclient on Windows 7 x64

♀尐吖头ヾ 提交于 2019-11-30 01:23:06
问题 I installed Python 2.7.8 and pgxn client. And I tried to run this statement from command line from the bin folder and path is setup correctly pgxnclient install http://api.pgxn.org/dist/pg_repack/1.2.1/pg_repack-1.2.1.zip But I got an error pg_config executable not found . 回答1: Bakground: pg_config is the configuration utility provided by PostgreSQL . This utility is used by various applications. Solution: Install PostgreSQL. Set the path. System Properties > Advanced PATH:C:\Program Files

Create a unique index on a non-unique column

痴心易碎 提交于 2019-11-29 23:50:39
问题 Not sure if this is possible in PostgreSQL 9.3+, but I'd like to create a unique index on a non-unique column. For a table like: CREATE TABLE data ( id SERIAL , day DATE , val NUMERIC ); CREATE INDEX data_day_val_idx ON data (day, val); I'd like to be able to [quickly] query only the distinct days. I know I can use data_day_val_idx to help perform the distinct search, but it seems this adds extra overhead if the number of distinct values is substantially less than the number of rows in the

Are postgres JSON indexes efficient enough compared with classic normalized tables?

会有一股神秘感。 提交于 2019-11-29 21:02:45
Current Postgresql versions have introduced various features for JSON content, but I'm concerned if I really should use them - I mean, there is not yet "best practice" estabilished on what works and what doesn't, or at least I can't find it. I have a specific example - I have a table about objects which, among other things, contains a list of alternate names for that object. All that data will also be included in a JSON column for retrieval purposes. For example (skipping all the other irrelevant fields). create table stuff (id serial primary key, data json); insert into stuff(data) values('{

Hidden Features of PostgreSQL [closed]

℡╲_俬逩灬. 提交于 2019-11-29 18:33:14
I'm surprised this hasn't been posted yet. Any interesting tricks that you know about in Postgres? Obscure config options and scaling/perf tricks are particularly welcome. I'm sure we can beat the 9 comments on the corresponding MySQL thread :) tommym Since postgres is a lot more sane than MySQL, there are not that many "tricks" to report on ;-) The manual has some nice performance tips. A few other performance related things to keep in mind: Make sure autovacuum is turned on Make sure you've gone through your postgres.conf (effective cache size, shared buffers, work mem ... lots of options

Laravel 5.5 why is exception not thrown by foreign key violation from delete method?

那年仲夏 提交于 2019-11-29 14:57:48
I have two tables in a Postgresql 9.3 database, companies and sites. There is a one-to-many relationship between them with companies on the one side. There is a foreign key constraint that prevents the deletion of a company if sites have been assigned to it. If I attempt to delete the company with the ID 'KSL' using an SQL query directly on the database, I get the expected error: ERROR: update or delete on table "companies" violates foreign key constraint "sites_company_id_fkey" on table "sites" DETAIL: Key (company_id)=(KSL) is still referenced from table "sites". I have defined an artisan

How to Auto Increment Alpha-Numeric value in postgresql?

喜夏-厌秋 提交于 2019-11-29 14:46:16
问题 I am using "PostgreSQL 9.3.5" I have a Table ( StackOverflowTable ) with columns (SoId,SoName,SoDob) . I want a Sequence generator for column SoId which is a Alpha-numeric value. I want to auto increment a Alpha-Numeric Value in postgresql. For eg : SO10001, SO10002, SO10003.....SO99999. Edit: If tomorrow i need to generate a Sequence which can be as SO1000E100, SO1000E101,... and which has a good performance. Then what is the best solution! 回答1: Use sequences and default value for id: