postgresql-9.1

Why is count(*) returning multiple results in this subquery (postgresql)?

放肆的年华 提交于 2019-12-13 18:01:23
问题 I want to group the results of a count(*) query into value buckets. I'm testing this on the dellstore2 postgresql sample database. My query below returns the right answers, but does so once for each row in the table (several thousand identical results). I can fix this by adding LIMIT 1 the the end of the query but I'd like to understand why I'm getting the duplicates in case it points to a wider problem with my approach. The query is: SELECT (SELECT count(*) FROM orders WHERE totalamount > 0

Rails Query to return users belongs to any cities & not belong to any cities

若如初见. 提交于 2019-12-13 15:55:57
问题 I have Many to Many Associations between two tables: For Ex users & cities users id name 1 Bob 2 Jon 3 Tom 4 Gary 5 Hary cities id name 1 London 2 New-york 3 Delhi users_cities id user_id city_id 1 1 2 2 2 1 3 3 1 4 3 2 5 4 3 I want two sql queries Query which accepts array of city_id and return all the users belongs to that cities. For Ex when city_id : [1, 2] then result should be O/P should be id name 1 Bob 2 Jon 3 Tom Query which accepts array of city_id and return all the users who do

Returning empty data from dynamic pivot is there is no data

左心房为你撑大大i 提交于 2019-12-13 06:25:18
问题 Code below from how to preserve column names on dynamic pivot is used to create dynamic pivot table. If source table contains no data, sql error occurs since create table column list end with comma (there are no pivot columns). How to fix this so that empty table is returned ? To reproduce, remove insert commands insert into sales values ( '2016-1-1', 'Ø 12.3/3mm', 2); insert into sales values ( '2016-1-1', '+-3,4%/3mm', 52); insert into sales values ( '2016-1-3', '/3,2m-', 246); from code.

Substract 2 hstore, You might need to add explicit type casts

家住魔仙堡 提交于 2019-12-13 05:21:11
问题 Documentation for postgres has hstore - hstore which delete matching pairs from left operand. Installed postgres extensions. When I try select public.hstore('"x"=>"30", "y"=>"c"') - public.hstore('"x"=>"30","y"=>"fred"') is error-ing with following ERROR: operator does not exist: public.hstore - public.hstore LINE 3: select public.hstore('"x"=>"30", "y"=>"c"') - public.hstore... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 回答1:

Get columns that differ between 2 rows

痞子三分冷 提交于 2019-12-13 04:32:15
问题 I have a table company with 60 columns. The goal is to create a tool to find, compare and eliminate duplicates in this table. Example: I find 2 companies that potentially are the same, but I need to know which values (columns) differ between these 2 rows in order to continue. I think it is possible to compare column by column x 60, but I search for a simpler and more generic solution. Something like: SELECT * FROM company where co_id=22 SHOW DIFFERENCE SELECT * FROM company where co_id=33 The

Preserve all elements of an array while (left) joining to a table

社会主义新天地 提交于 2019-12-13 04:19:38
问题 I'm trying to pull from my expenses table all 12 months expenses, group by month. If there are no results in one month, I still wish to receive this month worth 0 (zero) value result. My database is PostgreSQL 9.1. I wrote this query, but I don't succeed to execute it or understand how to correct it. SELECT fta.liability_id, fta.amount , date_part('month', fta.act_date) AS act_month FROM ( select i from generate_series(1,array_upper(ARRAY[1,2,3,4,5,6,7,8,9,10,11,12],1)) i) as ym LEFT OUTER

Multirow subselect as parameter to `execute using`

血红的双手。 提交于 2019-12-13 04:15:32
问题 The multirow subselect will be used in the right hand side of the in operator in the where clause: create table t (a integer); insert into t (a) values (1), (9); drop function if exists f(); create function f() returns void as $$ begin execute ' select a from t where a in $1 ' using (select 1 union select 2); end;$$ language plpgsql; select f(); ERROR: more than one row returned by a subquery used as an expression CONTEXT: SQL statement "SELECT (select 1 union select 2)" PL/pgSQL function "f"

Best practice to check if a variable exists in plpgsql?

夙愿已清 提交于 2019-12-13 02:57:40
问题 For instance I've got a stored procedure to import data from csv files and write the read data into a SQL table. I've got a table defined as below: CREATE TABLE person (id int, name text, age int, married boolean); First I check if the record exist already, if exists I do update, if doesn't - insert. Each field of record may have a different type, so the result of a SQL command are assigned to a list of scalar variables: SELECT name, age, married INTO v_name, v_age, v_married [..] Let's

Rails App on Heroku Cannot Write to PostgreSQL Database, Only Read

一笑奈何 提交于 2019-12-12 17:40:56
问题 I have a Ruby on Rails app that works fine locally with a sqlite3 database and can save and retrieve records without issue. When deployed to Heroku at http://moviedata.herokuapp.com/ using a postgresql database, records are not saving even though it looks like the logs say they are. Records read from the db fine and data is displayed as expected. The tailed logs for adding a record are: 2012-08-21T19:51:31+00:00 app[web.1]: 2012-08-21T19:51:31+00:00 app[web.1]: 2012-08-21T19:51:31+00:00 app

Create copy of PostgreSQL internal C function and load it as user defined function

天大地大妈咪最大 提交于 2019-12-12 14:06:05
问题 I would like to create modified version of C function int2_avg_accum located in src/backend/utils/adt/numeric.c . I thought I can (as a start) just compile numeric.h and load int2_avg_accum as user defined function. What I did: Added PG_MODULE_MAGIC to numeric.h (as described here) Renamed int2_avg_accum to int2_avg_accum2 in numeric.h Compiled numeric.h as described in the documentation (no errors, no warnings) ( cc -fpic -I pg_config --includedir-server -c numeric.c and then cc -shared -o