niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » Oracle
aggregate and update
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
Jan van Veldhuizen
*nix forums beginner


Joined: 03 Feb 2005
Posts: 8

PostPosted: Fri Feb 04, 2005 7:27 am    Post subject: Re: aggregate and update Reply with quote

Quote:
Can't you simply use NVL to convert null sums to zero?

No. There's no null sum....


I want only update the counters for the names currently in the test table.
The contents of that table can be different tomorrow, with other names, and
then the counters has to be updated (accumulated) again

In the mean time I have tested what happens with my statement. It looks like
the aggregate query is executed only once. The optimizer is very smart I
think....
Back to top
Malcolm Dew-Jones
*nix forums Guru


Joined: 04 Mar 2005
Posts: 418

PostPosted: Thu Feb 03, 2005 11:38 pm    Post subject: Re: aggregate and update Reply with quote

Jan van Veldhuizen (jan@van-veldhuizen.nl) wrote:
: I am trying to update a table with aggregate values without creating a view.
: I am having more complicated tables, but I will show what am doing with some
: simple tables:

: create table counters (name varchar(10), cnt int);
: create table test (name varchar(10), val int);

: insert into counters values('john', 0);
: insert into counters values('peter', 0);
: insert into counters values('david', 0);

: insert into test values('john', 1);
: insert into test values('john', 3);
: insert into test values('peter', 10);

: I want to calculate the sum of the val column and store it in the cnt column
: of the counters table.
: And I want the cnt value of David to remain zero, and not nullified because
: it does not exists in the test table.

Can't you simply use NVL to convert null sums to zero?

--

This space not for rent.
Back to top
Jan van Veldhuizen
*nix forums beginner


Joined: 03 Feb 2005
Posts: 8

PostPosted: Thu Feb 03, 2005 10:08 pm    Post subject: Re: aggregate and update (correction) Reply with quote

"Jan van Veldhuizen" <jan@van-veldhuizen.nl> wrote in message
news:4202ae5f$0$28980$e4fe514c@news.xs4all.nl...
Quote:
I am trying to update a table with aggregate values without creating a
view.
I am having more complicated tables, but I will show what am doing with
some
simple tables:

create table counters (name varchar(10), cnt int);
create table test (name varchar(10), val int);

insert into counters values('john', 0);
insert into counters values('peter', 0);
insert into counters values('david', 0);

insert into test values('john', 1);
insert into test values('john', 3);
insert into test values('peter', 10);

I want to calculate the sum of the val column and store it in the cnt
column
of the counters table.
And I want the cnt value of David to remain zero, and not nullified
because
it does not exists in the test table.

If a create a view it is simple:

create view myview as select name, sum(val) mysum from test group by name;

update counters set cnt = (select mysum from myview where myview.name =
counters.name)
where exists (select null from myview where myview.name = counters.name)

But now without the view:

update counters set cnt =
(select mysum from (select name, sum(val) mysum from test group by name)
vw
where vw.name = counters.name)
where exists
(select null from (select name, sum(val) mysum from test group by name) vw
where vw.name = counters.name)

My question is: is this inefficient because it is creating the view vw
twice?

The advantage is: I can use this syntax in both Oracle and SqlServer, so I
can avoid to write separate statements for both databases. I have an
application with sql embedded in the source, so I am trying to keep the
sql
as universal as possible.












Back to top
Jan van Veldhuizen
*nix forums beginner


Joined: 03 Feb 2005
Posts: 8

PostPosted: Thu Feb 03, 2005 10:04 pm    Post subject: aggregate and update Reply with quote

I am trying to update a table with aggregate values without creating a view.
I am having more complicated tables, but I will show what am doing with some
simple tables:

create table counters (name varchar(10), cnt int);
create table test (name varchar(10), val int);

insert into counters values('john', 0);
insert into counters values('peter', 0);
insert into counters values('david', 0);

insert into test values('john', 1);
insert into test values('john', 3);
insert into test values('peter', 10);

I want to calculate the sum of the val column and store it in the cnt column
of the counters table.
And I want the cnt value of David to remain zero, and not nullified because
it does not exists in the test table.

If a create a view it is simple:

create view myview as select name, sum(val) mysum from test group by name;

update counters set cnt = (select mysum from myview where myview.name =
counters.name)
where exists (select null from myview where myview.name = counters.name)

But now without the view:

update counters set cnt =
(select mysum from (select name, sum(val) som from test group by name) vw
where vw.name = counters.name)
where exists
(select null from (select name, sum(val) som from test group by name) vw
where vw.name = counters.name)

My question is: is this inefficient because it is creating the view vw
twice?

The advantage is: I can use this syntax in both Oracle and SqlServer, so I
can avoid to write separate statements for both databases. I have an
application with sql embedded in the source, so I am trying to keep the sql
as universal as possible.
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 3:11 am | All times are GMT
navigation Forum index » Databases » Oracle
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts C# MSSQL2000 update pln C 5 Fri Jul 21, 2006 7:41 am
No new posts HPSBMA02133 SSRT061201 rev.1 - HP Oracle for OpenView (Of... Security Alert HP-UX 0 Thu Jul 20, 2006 6:43 pm
No new posts HPSBMA02133 SSRT061201 rev.1 - HP Oracle for OpenView (Of... Security Alert HP-UX 0 Thu Jul 20, 2006 5:53 pm
No new posts Problem w/ Yast2, can't install new software or update Emmanuel Durand Suse 0 Thu Jul 20, 2006 5:50 am
No new posts HOW-TO Full update from 5.3 to 6.0-6.1 ? Vlad Mfk FreeBSD 2 Wed Jul 19, 2006 5:52 am

Loans | 0 Credit Cards | Debt Consolidation | Credit Card Consolidation | Loans
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1263s ][ Queries: 20 (0.0422s) ][ GZIP on - Debug on ]