473,750 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems requiring a GROUP BY clause on update?

I have a query that is asking me to GROUP a column, yet when I GROUP it it
causes an error near GROUP.
What is very strange about the following query is that the line

list_size_activ e = COUNT(NEW.activ e)

PROPERLY sets the value to the number of new items.

However,

list_size_activ e = list_size_activ e + COUNT(NEW.activ e)

Gives and error about needing to group the column and

list_size_activ e = list_size_activ e + ( SELECT COUNT(NEW.activ e) )

Only increments the value by one while the first only assigning statement
actually assigns it to the number of new items.

Here is what I have tried so far with varying results. I am totally out of
ideas beyond this :(

Attribute lists.list_size _active must be GROUPed or used in an aggregate
function

CREATE OR REPLACE RULE items_log_inser t AS ON INSERT
TO listdb.list_ite ms
WHERE NEW.active = 1 DO (
UPDATE lists SET
list_size_activ e = list_size_activ e + COUNT( NEW.active )
WHERE list_id = NEW.list_id;
);


This function sets the value to the appropriate number, but fails to increment
it as needed proving that the number of items is attainable.

CREATE OR REPLACE RULE items_log_inser t AS ON INSERT
TO listdb.list_ite ms
WHERE NEW.active = 1 DO (
UPDATE lists SET
list_size_activ e = COUNT( NEW.active )
WHERE list_id = NEW.list_id;
);


This function does not shoot any errors off when I create the RULE.

However, it sets list_size_activ e to 1 no matter how many rows are in NEW.

CREATE OR REPLACE RULE items_log_inser t AS ON INSERT
TO listdb.list_ite ms
WHERE NEW.active = 1 DO (
UPDATE lists SET
list_size_activ e = list_size_activ e + ( SELECT COUNT( NEW.active ) )
WHERE list_id = NEW.list_id;
);

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 11 '05 #1
2 5840
Joshua Moore-Oliva <jo**@chatgris. com> writes:
Attribute lists.list_size _active must be GROUPed or used in an aggregate
function CREATE OR REPLACE RULE items_log_inser t AS ON INSERT
TO listdb.list_ite ms
WHERE NEW.active = 1 DO (
UPDATE lists SET
list_size_activ e = list_size_activ e + COUNT( NEW.active )
WHERE list_id = NEW.list_id;
);


The error message isn't really helpful, perhaps, but I think the system
is quite right to squawk. What do you expect that command to do? The
COUNT() is completely meaningless because there isn't anything for it
to iterate over. (The SQL spec forbids aggregate functions in UPDATE
lists altogether, and I rather think they are right, though we've not
yet got around to installing that specific error check.)

Possibly what you want is some kind of sub-select:

UPDATE lists SET
list_size_activ e = list_size_activ e +
(SELECT COUNT(*) FROM ... WHERE ...)
WHERE list_id = NEW.list_id;

but I can't help you with what to put for "..." because you've not made
it clear what you are trying to achieve.

It's also entirely likely that you'd find an ON INSERT trigger to be
easier to work with than a rule. People frequently try to force rules
to behave like per-tuple actions, but they almost always lose the
battle. A rule is a query-level transformation, and it requires a
different mindset to use effectively.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 11 '05 #2
I am attempting to find out how many rows a query operation affected, then
update a separate row to keep count, because running a SELECT COUNT(id) FROM
lists is far too slow when there are millions of records and the number if
required for a user interface.

So far my only success has only come from making functions for each operation,
and using GET DIAGNOSTICS num_affected := ROW_COUNT;

I don't want triggers to update one at a time because I at times insert
millions of rows, and don't think that millions of update statements is too
healthy :).

Thanks, Josh.
On September 17, 2003 11:23 am, Tom Lane wrote:
Joshua Moore-Oliva <jo**@chatgris. com> writes:
Attribute lists.list_size _active must be GROUPed or used in an aggregate
function

CREATE OR REPLACE RULE items_log_inser t AS ON INSERT
TO listdb.list_ite ms
WHERE NEW.active = 1 DO (
UPDATE lists SET
list_size_activ e = list_size_activ e + COUNT( NEW.active )
WHERE list_id = NEW.list_id;
);


The error message isn't really helpful, perhaps, but I think the system
is quite right to squawk. What do you expect that command to do? The
COUNT() is completely meaningless because there isn't anything for it
to iterate over. (The SQL spec forbids aggregate functions in UPDATE
lists altogether, and I rather think they are right, though we've not
yet got around to installing that specific error check.)

Possibly what you want is some kind of sub-select:

UPDATE lists SET
list_size_activ e = list_size_activ e +
(SELECT COUNT(*) FROM ... WHERE ...)
WHERE list_id = NEW.list_id;

but I can't help you with what to put for "..." because you've not made
it clear what you are trying to achieve.

It's also entirely likely that you'd find an ON INSERT trigger to be
easier to work with than a rule. People frequently try to force rules
to behave like per-tuple actions, but they almost always lose the
battle. A rule is a query-level transformation, and it requires a
different mindset to use effectively.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 11 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2957
by: | last post by:
Hello, Sorry to ask what is probably a simple answer, but I am having problems updating a table/database from a PHP/ PHTML file. I can Read From the Table, I can Insert into Table/Database, But not update. Here is a copy of the script I am using. I do not know what version of MySQL my host is running nor do I have Shell Access to it. I
0
1010
by: Ray Gurganus | last post by:
It seems like I remember an option that when enabled, will require UPDATE and DELETE statements to have a WHERE clause, to avoid destructive oversights? Now when looking for that, I can't find it. Maybe I am thinking of a different system. If it exists, can someone point me to it? If it doesn't exist, maybe this is something that could be added in. Thanks.
3
17373
by: Robby McGehee | last post by:
I need this to work: SELECT FROM WITH (NOLOCK) where ='a' GROUP BY , HAVING COUNT () > 1 The problem is that I get an error that needs to be in the GROUP BY clause or aggregate function. if I put it in there, I will get no duplicates (because it is the identity field). The whole point of this is to find dups. Thanks for any help.
3
14884
by: Michel | last post by:
Hi All, In Oracle, I can easily make this query : UPDATE t1 SET (f1,f2)=(SELECT AVG(f3),SUM(f4) FROM t2 WHERE t2.f5=t1.f6) WHERE f5='Something' I cannot seem to be able to do the same thing with MS-SQL. There are
4
28824
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the select statement). I'm mostly a SQL hobbiest, so it's possible that I am not doing this in the most efficient manner. Anyone care to comment on this with relation to the following example (is there a way to acheive this without re-stating the...
9
3306
by: cyrus.kapadia | last post by:
Let's say I have the following table: entry product quality 1 A 80 2 A 70 3 A 80 4 B 60 5 B 90 6 C 80 7 D 80
2
2362
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20 00:00:00.000 Subject_1 877 1900-01-01 00:00:00.000 Subject_2 877 2004-12-20 00:00:00.000 Subject_3
4
1609
by: ree32 | last post by:
I know when you are using group by functions you have to include all the columns in the GROUP BY clause. But what I am having problems when using a case statement to determine whether to sum of not a column. eg. SELECT Country, Case WHEN Age<15 THEN Sum(Income) ELSE NULL END AS YouthIncome, Case WHEN Age>65 THEN Sum(Income) ELSE NULL END AS PensionIncome FROM WORLDTABLE
9
11085
by: Acupuncture | last post by:
Hi, I am developing a JDBC application and I encountered this problem (DB2 for ISeries). I want to do a select for update and also use the fetch first rows clause. This is my sql statement: SELECT UsrNbr, UsrPwd, UsrPwdChgD, UsrEmail, UsrChgUsrI, UsrChgDte FROM USERS WHERE UsrNbr = ? FETCH FIRST 1 ROWS ONLY FOR UPDATE This runs fine (the cursor gets this name 'P00022'), but when I execute the
0
9001
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9397
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9344
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9257
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8264
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6810
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.