pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael J Davis <michael.j.davis@tvguide.com>
To: 'M Simms' <grim@argh.demon.co.uk>
To: pgsql-sql@postgreSQL.org
Subject: RE: [SQL] Update problem I cannot work out
Date: Sat, 1 May 1999 22:13:16 -0500
Message-ID: <93C04F1F5173D211A27900105AA8FCFC145461@lambic.prevuenet.com> (raw)

	Try something like:

	Create function new_function(value_data_type)
		'Select sum(t1.var1) from t1 where t1.var2 = $1'  LANGUAGE
'sql';

	update t2 set var1=t2.var1+new_function(t2.var2);


	-----Original Message-----
	From:	M Simms [SMTP:grim@argh.demon.co.uk]
	Sent:	Saturday, May 01, 1999 8:26 PM
	To:	pgsql-sql@postgreSQL.org
	Subject:	[SQL] Update problem I cannot work out

	I cannot work out how to do the following query

	testdb=> select * from t1;
	var1|var2
	----+----
	   1|   2
	   2|   2
	   2|   3
	(3 rows)

	testdb=> select * from t2;
	var1|var2
	----+----
	   5|   2
	   9|   3
	   4|   4
	(3 rows)

	I need to add to the value of each var1 in t2 the sum of all var1's
in t1
	that have the same value corresponding var2's

	testdb=> update t2 set var1=t2.var1+sum(t1.var1) where
t1.var2=t2.var2;
	ERROR:  parser: illegal use of aggregates or non-group column in
target list

	I imagine that this is failing because it isnt sure which values to
	sum(), but I cannot for the life of me figure out how to solve this.

	My second idea was to do

	testdb=> update t2 set var1=t2.var1+t1.var1 where t2.var2=t1.var1;
	NOTICE:  Non-functional update, only first update is performed
	UPDATE 1

	This will only, obviously, add one of the values I need to add.

	The values I need to end up with, just so you know you are solving
the
	right problem, are:

	testdb=> select * from t1;
	var1|var2
	----+----
	   1|   2
	   2|   2
	   2|   3
	(3 rows)

	testdb=> select * from t2;
	var1|var2
	----+----
	   8|   2
	  11|   3
	   4|   4
	(3 rows)

	Any help on this would be appreciated. I am a bit stuck here, and I
am
	sure I am just missing something blindingly obvious.
	I am loathe to use a cursor to do this, because my actual dataset is
	upwards of 10,000 records, and I do not wish to do 10,000 different
	queries.



view thread (4+ messages)  latest in thread

Message-ID: <93C04F1F5173D211A27900105AA8FCFC145461@lambic.prevuenet.com>
Permalink:  ../93C04F1F5173D211A27900105AA8FCFC145461@lambic.prevuenet.com/
Also on:    postgresql.org/message-id/93C04F1F5173D211A27900105AA8FCFC145461@lambic.prevuenet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: michael.j.davis@tvguide.com, grim@argh.demon.co.uk
  Subject: Re: [SQL] Update problem I cannot work out
  In-Reply-To: <93C04F1F5173D211A27900105AA8FCFC145461@lambic.prevuenet.com>
  References: <93C04F1F5173D211A27900105AA8FCFC145461@lambic.prevuenet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox