postgresql-9.1

how to preserve column names on dynamic pivot

笑着哭i 提交于 2020-01-17 06:19:29
问题 Sales data contains dynamic product names which can contian any characters. Dynamic pivot table is created based on sample from Crosstab with a large or undefined number of categories translate() is used to remove bad characters. In result pivot table column names are corrupted: missing characters and spaces are removed. How to return data with same column names as in source data ? I tried to use quote_ident(productname) as tootjakood, instead of 'C'||upper(Translate(productname,'Ø. &/+-,%',

Retrieve multiple rows with query using AND and OR

不问归期 提交于 2020-01-16 00:19:26
问题 I want to retrieve multiple rows using same id's. Therefore having this table "component_property", I would like to have as results 2 records, id's: 8 and 9 according to my SQL query (check below), but of course and retrieving nothing since I'm checking if cp.property_id = 9102 and later and checking if cp.property_id = 8801 which at the same time is impossible. ID;type_id;name;desc;property_id,value -------------------------------------- 8;3832;"amplifier1";"";8801;"3" 8;3832;"amplifier1";""

how to execute sql queries from a shell script

天涯浪子 提交于 2020-01-15 03:23:26
问题 I am writing a shell script to run bunch of queries in postgres. This is the script that I have written. #!/bin/sh host="172.16.7.102" username="priyank" dbname="truaxis" x=1 echo "Connection done" while [ $x -le 3 ] do x=$(($x + 1 )) echo "Connection $x" psql -h $host $dbname $username << EOF select * from students where sid = $x; EOF done There are two issues with this script. pgtest1.sh: 17: Syntax error: end of file unexpected (expecting "done") how do I pass $x in the sql dynamically I

how to collect multiple values as a single string in postgres?

瘦欲@ 提交于 2020-01-14 09:38:09
问题 I have tables : Project table id name ------- 1 A 2 B Assignment table id name project_id ------------------- 1 A1 1 2 A2 1 3 A3 2 I wish to write a query that returns each project with the name of the assignments created from it, like : project_id assignments ----------------------- 1 A1,A2 2 A3 Is there any way to achieve that ? 回答1: You can join the tables and use array_agg to combine the values separated by a comma SELECT a.id, array_agg(b.name) assignments FROM Project a INNER JOIN

Understanding how to optimize a query via the Postgres/rails explain data

十年热恋 提交于 2020-01-13 16:57:32
问题 I have the following query: c = Invite.where(:invite_method => 'email', :email => email, :created_at => Time.zone.now.beginning_of_day..Time.zone.now.end_of_day).count This query is taking some time as the table has 1m+ records. Here is the query output: > invites_sent_today = Invite.where(:invite_method => 'email', :email => email, :created_at => Time.zone.now.beginning_of_day..Time.zone.now.end_of_day).exp Invite Load (62.3ms) SELECT "invites".* FROM "invites" WHERE "invites"."invite_method

Understanding how to optimize a query via the Postgres/rails explain data

烈酒焚心 提交于 2020-01-13 16:57:20
问题 I have the following query: c = Invite.where(:invite_method => 'email', :email => email, :created_at => Time.zone.now.beginning_of_day..Time.zone.now.end_of_day).count This query is taking some time as the table has 1m+ records. Here is the query output: > invites_sent_today = Invite.where(:invite_method => 'email', :email => email, :created_at => Time.zone.now.beginning_of_day..Time.zone.now.end_of_day).exp Invite Load (62.3ms) SELECT "invites".* FROM "invites" WHERE "invites"."invite_method

WHERE NOT EXISTS in PostgreSQL gives syntax error

随声附和 提交于 2020-01-13 09:36:08
问题 When trying to use the WHERE NOT EXISTS clause to prevent adding a row with a duplicate value in the column age , I get the error syntax error at or near "WHERE" . Why did it throw a syntax error? I'm using Postgresql 9.1. SQL INSERT INTO live.users ("website", "age") values ('abc', '123') WHERE NOT EXISTS (SELECT age FROM live.users WHERE age = 123); Error ERROR: syntax error at or near "WHERE" LINE 6: WHERE NOT EXISTS (SELECT age FROM live.users W... 回答1: Do instead: INSERT INTO live.users

How to duplicate schemas in PostgreSQL

我的梦境 提交于 2020-01-11 17:29:49
问题 I have a database with schema public and schema_A . I need to create a new schema schema_b with the same structure than schema_a . I found the function below, the problem is that it does not copy the foreign key constraints. CREATE OR REPLACE FUNCTION clone_schema(source_schema text, dest_schema text) RETURNS void AS $BODY$ DECLARE object text; buffer text; default_ text; column_ text; BEGIN EXECUTE 'CREATE SCHEMA ' || dest_schema ; -- TODO: Find a way to make this sequence's owner is the

Postgres function returning table not returning data in columns

人盡茶涼 提交于 2020-01-08 21:51:15
问题 I have a Postgres function which is returning a table: CREATE OR REPLACE FUNCTION testFunction() RETURNS TABLE(a int, b int) AS $BODY$ DECLARE a int DEFAULT 0; DECLARE b int DEFAULT 0; BEGIN CREATE TABLE tempTable AS SELECT a, b; RETURN QUERY SELECT * FROM tempTable; DROP TABLE tempTable; END; $BODY$ LANGUAGE plpgsql; This function is not returning data in row and column form. Instead it returns data as: (0,0) That is causing a problem in Coldfusion cfquery block in extracting data. How do I

Postgres function returning table not returning data in columns

我的未来我决定 提交于 2020-01-08 21:47:27
问题 I have a Postgres function which is returning a table: CREATE OR REPLACE FUNCTION testFunction() RETURNS TABLE(a int, b int) AS $BODY$ DECLARE a int DEFAULT 0; DECLARE b int DEFAULT 0; BEGIN CREATE TABLE tempTable AS SELECT a, b; RETURN QUERY SELECT * FROM tempTable; DROP TABLE tempTable; END; $BODY$ LANGUAGE plpgsql; This function is not returning data in row and column form. Instead it returns data as: (0,0) That is causing a problem in Coldfusion cfquery block in extracting data. How do I