postgresql-9.3

Passing the table as a parameter

本秂侑毒 提交于 2019-12-20 06:47:38
问题 I have to convert from lat and long to geom to use PostGIS. My problem, I have various tables from different locations and I want to pass the table as a parameter to the function. I'm trying this: CREATE or REPLACE FUNCTION convert_from_lon_lat(float,float,character varying) RETURNS integer AS $$ select id from $3 as vertices order by vertices.geom <-> ST_SetSrid(ST_MakePoint($1,$2),4326) LIMIT 1; $$ LANGUAGE SQL; but I get a syntax error. EDIT1: So I changed the previous code to this: CREATE

How to create columns for different fields without applying the pivoting function

故事扮演 提交于 2019-12-20 06:16:27
问题 So i have a question for the tough hearted! I have had an issue with this concept for awhile and I need it to be cleared... The following code shows students who have more than 1 language SELECT DISTINCT s.studentnumber as studentnr, p.firstname AS name, sl.gradenumber as gradenumber, string_agg(DISTINCT l.text, ', ') as languages FROM student s JOIN pupil p ON p.id = s.pupilid JOIN pupillanguage pl on pl.pupilid = p.id JOIN language l on l.id = pl.languageid JOIN schoollevel sl ON sl.id = p

Implement search filter for all columns

有些话、适合烂在心里 提交于 2019-12-20 05:45:40
问题 I found this search example in PostgreSQL http://www.postgresql.org/docs/current/interactive/textsearch-tables.html#TEXTSEARCH-TABLES-SEARCH I tried to implement this code for this table this way: 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) ; Java code public List

Counting distinct rows using recursive cte over non-distinct index

試著忘記壹切 提交于 2019-12-20 03:18:15
问题 Given the following schema: CREATE TABLE identifiers ( id TEXT PRIMARY KEY ); CREATE TABLE days ( day DATE PRIMARY KEY ); CREATE TABLE data ( id TEXT REFERENCES identifiers , day DATE REFERENCES days , values NUMERIC[] ); CREATE INDEX ON data (id, day); What is the best way to count all distinct days between two timestamps? I've tried the following two methods: EXPLAIN ANALYZE SELECT COUNT(DISTINCT day) FROM data WHERE day BETWEEN '2010-01-01' AND '2011-01-01'; QUERY PLAN --------------------

Specifying data type of columns in a values subquery

烈酒焚心 提交于 2019-12-20 02:36:09
问题 Is there a way to specify the data types of the columns in a values subquery? Something like (values (...)) as tmp_name (colname::type) ? Contrived Example Say I have a table with a uuid column: /* setup */ create table foo (id uuid); insert into foo values ('aaaabbbbccccddddeeeeffff00001111'), ('aaaabbbbccccddddeeeeffff00002222'), ('aaaabbbbccccddddeeeeffff00003333'); If I try to join against it using a values subquery, postgres assumes the column is type text and throws a type error (even

Postgresql - INSERT RETURNING INTO ambiguous column reference

£可爱£侵袭症+ 提交于 2019-12-19 11:55:09
问题 Can someone politely explain this crazyness? INSERT INTO "dbo"."UserProfile" ("FirstName") VALUES('John') RETURNING "UserProfileId" INTO _UserProfileId; throws an ambiguous reference error, however this correctly executes: INSERT INTO "dbo"."UserProfile" ("FirstName") VALUES('John') RETURNING "dbo"."UserProfile"."UserProfileId" INTO _UserProfileId; _UserProfileId is a declared integer variable. I couldn't find any references to this syntax in the manual or why on earth this would be ambiguous

Invalid count and sum in cross tab query using PostgreSQL

为君一笑 提交于 2019-12-19 09:43:02
问题 I am using PostgreSQL 9.3 version database. I have a situation where I want to count the number of products sales and sum the amount of product and also want to show the cities in a column where the product have sale. Example Setup create table products ( name varchar(20), price integer, city varchar(20) ); insert into products values ('P1',1200,'London'), ('P1',100,'Melborun'), ('P1',1400,'Moscow'), ('P2',1560,'Munich'), ('P2',2300,'Shunghai'), ('P2',3000,'Dubai'); Crosstab query : select *

Check if key exists in a JSON with PL/pgSQL?

一世执手 提交于 2019-12-19 08:15:15
问题 This question was migrated from Database Administrators Stack Exchange because it can be answered on Stack Overflow. Migrated 4 years ago . I'm trying to check if a key exists in a JSON sent as parameter in a PL/pgSQL function. Here is the function. CREATE FUNCTION sp_update_user(user_info json) RETURNS json LANGUAGE plpgsql AS $$ BEGIN PERFORM user_info->>'lastname'; IF FOUND THEN UPDATE users SET lastname = user_info->>'lastname' WHERE id = sp_update_user.user_id; END IF; PERFORM user_info-

Check if key exists in a JSON with PL/pgSQL?

我只是一个虾纸丫 提交于 2019-12-19 08:14:10
问题 This question was migrated from Database Administrators Stack Exchange because it can be answered on Stack Overflow. Migrated 4 years ago . I'm trying to check if a key exists in a JSON sent as parameter in a PL/pgSQL function. Here is the function. CREATE FUNCTION sp_update_user(user_info json) RETURNS json LANGUAGE plpgsql AS $$ BEGIN PERFORM user_info->>'lastname'; IF FOUND THEN UPDATE users SET lastname = user_info->>'lastname' WHERE id = sp_update_user.user_id; END IF; PERFORM user_info-

Trigger with dynamic field name

大兔子大兔子 提交于 2019-12-19 07:43:24
问题 I have a problem on creating PostgreSQL (9.3) trigger on update table. I want set new values in the loop as EXECUTE 'NEW.'|| fieldName || ':=''some prepend data'' || NEW.' || fieldName || ';'; where fieldName is set dynamically. But this string raise error ERROR: syntax error at or near "NEW" How do I go about achieving that? 回答1: You can implement that rather conveniently with the hstore operator #= : Make sure the additional module is installed properly ( once per database), in a schema