postgresql-9.1

How to allow only one row for a table?

三世轮回 提交于 2019-12-22 03:48:10
问题 I have one table in which I would like only one entry . So if someone is trying to insert another row it shouldn't be allowed, only after someone deleted the previously existing row. How do I set a rule for a table like this? 回答1: A UNIQUE constraint allows multiple rows with NULL values, because two NULL values are never considered to be the same. Similar considerations apply to CHECK constraints. They allow the expression to be TRUE or NULL (just not FALSE ). Again, NULL values get past the

Java queries against PGPool II cause “unnamed prepared statement does not exist” errors

不打扰是莪最后的温柔 提交于 2019-12-22 03:22:09
问题 I have a Java app that uses a Postgres database and I'm trying to introduce PGPool in order to scale up my database. I'm running into a problem where Postgres throws the following error: unnamed prepared statement does not exist . After cranking up the logging on Postgres I see the following stuff happening for every select statement my app executes: EDTLOG: 00000: duration: 7.585 ms parse <unnamed>: "my select statement here" EDTLOG: 00000: duration: 0.088 ms bind <unnamed>: "my select

Group by end of period instead of start date

我怕爱的太早我们不能终老 提交于 2019-12-21 22:42:22
问题 I'm looking to aggregate data by the end date of a dataset with some leading period rather than the start. For example, I want to query a table and return the count of matching results 30 days PRIOR to the end date of the date shown in the results. The original table would contain ONLY the date a sale was made (timestamp). Example: sales_timestamp ------------------ 2015-08-05 12:00:00 2015-08-06 13:00:00 2015-08-25 12:31:00 2015-08-26 01:02:00 2015-08-27 02:03:00 2015-08-29 04:23:00 2015-09

PostgreSQL crosstab/pivot problems

旧时模样 提交于 2019-12-21 21:39:42
问题 I have a prefs table, and here are the relevant columns: mydb=> SELECT pref_id, pref_name, pref_value FROM prefs; pref_id | pref_name | pref_value ---------+--------------+---------------- 1 | PagerNumber | 2125551234 2 | PagerCarrier | @att.com 3 | PagerCarrier | @something.com I want to produce something like this: section | pager_number | pager_carrier ---------+----------------+--------------- 1 | 2125551234 | 2 | | @att.com 3 | | @something.com So I used crosstab, following this example

Postgresql : How do I select top n percent(%) entries from each group/category

最后都变了- 提交于 2019-12-21 17:25:36
问题 We are new to postgres, we have following query by which we can select top N records from each category. create table temp ( gp char, val int ); insert into temp values ('A',10); insert into temp values ('A',8); insert into temp values ('A',6); insert into temp values ('A',4); insert into temp values ('B',3); insert into temp values ('B',2); insert into temp values ('B',1); select a.gp,a.val from temp a where a.val in ( select b.val from temp b where a.gp=b.gp order by b.val desc limit 2);

JPA with Hibernate 3.6.8.Final, PostgreSQL 9.1, SQLGrammarException - configuration issue? Weird SQL statement

不羁岁月 提交于 2019-12-21 17:22:34
问题 Edit : SOLVED Right. I found the thing that confused me. I use pgadmin to create tables and others database internals, checked right now: if at least one letter in the name (table name, column name, pk name, etc) is in the upper case, then pgadmin uses it in the SQL creation script as it is, using double quotes, so PostgreSQL interprets the name as it was written. If run the following script: CREATE TABLE SAMPLE ( ID integer NOT NULL, TITLE character varying(100) NOT NULL, CONSTRAINT SAMPLE

PostgreSQL: joining arrays within group by clause

泄露秘密 提交于 2019-12-21 12:57:53
问题 We have a problem grouping arrays into a single array. We want to join the values from two colums into one single array and aggregate these arrays of multiple rows. Given the following input: | id | name | col_1 | col_2 | | 1 | a | 1 | 2 | | 2 | a | 3 | 4 | | 4 | b | 7 | 8 | | 3 | b | 5 | 6 | We want the following output: | a | { 1, 2, 3, 4 } | | b | { 5, 6, 7, 8 } | The order of the elements is important and should correlate with the id of the aggregated rows. We tried the array_agg function

Convert hex string to bigint in Postgres [duplicate]

左心房为你撑大大i 提交于 2019-12-21 05:43:44
问题 This question already has answers here : Convert hex in text representation to decimal number (5 answers) Closed 2 years ago . I'd like to convert a hex string as used by HTML into a bigint to then convert it into separate R, G and B values in Postgres via a function written in PL/pgSQL. I can decode the string into bytea like this: hex bytea := decode(hex, 'hex'); And in a query with fixed values this works like a beauty: select ( array[ (cast(x'ffaa33' as bigint) >> 16) % 256, (cast(x

How to use Entity Framework + PostgreSQL from connection?

妖精的绣舞 提交于 2019-12-21 04:29:14
问题 I have already seen threads discussing the use of Entity Framework and PostgreSQL with official instructions. Those instructions need to run gacutil for every install which is not so handy for deployment purposes. What I want to do here is passing PostgreSQL connection directly to the DbContext constructor. This is enough for me because I am going to use CodeFirst without designer. This is what I do: public class Context : DbContext { Context(System.Data.Common.DbConnection connection) : base

Aggregate values over a range of hours, every hour

爷,独闯天下 提交于 2019-12-20 18:22:01
问题 I have a PostgreSQL 9.1 database with a table containing a timestamp and a measuring value '2012-10-25 01:00' 2 '2012-10-25 02:00' 5 '2012-10-25 03:00' 12 '2012-10-25 04:00' 7 '2012-10-25 05:00' 1 ... ... I need to average the value over a range of 8 hours, every hour. In other words, I need the average of 1h-8h, 2h-9h, 3h-10h etc. I have no idea how to proceed for such a query. I have looked everywhere but have also no clue what functionalities to look for. The closes I find are hourly/daily