SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Aggregate function error - help please!
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mitin
Yak Posting Veteran

81 Posts

Posted - 02/11/2013 :  17:34:26  Show Profile  Reply with Quote
Hi,

I have a query that I need to run on both a SQL 2008 DB and also a SQL 2000 DB.

When I run the query on the 2008 DB, no errors are returned, however in SQL 2000 I get an error. The tables in each database are similar but not exactly the same, however they both have the fields that are being selected in the query and I can't understand why one DB is giving this error whilst for the other DB the query works perfectly.

The query is below:

use database

select distinct 
servername,
firstoccurrence,
lastoccurrence,
eventid,
key,
group,
node,
summary
sum(tally) as NumberofEvents,
--sum(count(*)) over(partition by dateadd(dd,datediff(dd,0,lastoccurrence),0)) as daytotal
sum(count(*)) over(partition by servername, dateadd(dd,datediff(dd,0,lastoccurrence),0)) as daytotalbyserver

from databasetable
where firstoccurrence >= '2013-02-04' and firstoccurrence < '2013-02-08'
and lastoccurrence >= '2013-02-04' and lastoccurrence < '2013-02-08'

group by
firstoccurrence,
lastoccurrence,
servername,
eventid,
alertkey,
alertgroup,
node,
summary
and the error that I get is below:
Server: Msg 130, Level 15, state 1, Line 14. Cannot 
perform an aggregate function on an expression containing an aggregate or sub query
What is the issue? its really bugging me. Any help greatly appreciated!

Thanks





Edited by - mitin on 02/11/2013 17:39:15

Lamprey
Flowing Fount of Yak Knowledge

4604 Posts

Posted - 02/11/2013 :  17:43:27  Show Profile  Reply with Quote
The Error tells you: you cannot aggregate and aggregate. For example:
DECLARE @Foo TABLE (ID INT, Val INT)
INSERT @Foo
VALUES
(1, 11),
(2, 12),
(3, 13),
(4, 14),
(5, 15),
(1, 16),
(2, 17),
(3, 18),
(4, 19),
(5, 20)

SELECT ID, SUM(COUNT(*))
FROM @Foo
GROUP BY ID
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 02/11/2013 :  22:59:23  Show Profile  Reply with Quote
you need to find the count(*) first in subquery and then apply sum over it.

ie like

use database

select *,
sum(cnt) over(partition by dateadd(dd,datediff(dd,0,lastoccurrence),0)) as daytotal,
sum(cnt) over(partition by servername, dateadd(dd,datediff(dd,0,lastoccurrence),0)) as daytotalbyserver
from
(
select distinct 
servername,
firstoccurrence,
lastoccurrence,
eventid,
key,
group,
node,
summary
sum(tally) as NumberofEvents,
count(*) as cnt
from databasetable
where firstoccurrence >= '2013-02-04' and firstoccurrence < '2013-02-08'
and lastoccurrence >= '2013-02-04' and lastoccurrence < '2013-02-08'

group by
firstoccurrence,
lastoccurrence,
servername,
eventid,
alertkey,
alertgroup,
node,
summary
)t


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mitin
Yak Posting Veteran

81 Posts

Posted - 02/12/2013 :  05:47:15  Show Profile  Reply with Quote
Why would it work on one database and not the other?

I don't understand this....

thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22742 Posts

Posted - 02/12/2013 :  05:57:58  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by mitin

Why would it work on one database and not the other?

I don't understand this....

thanks


Which RDBMS did it work in?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mitin
Yak Posting Veteran

81 Posts

Posted - 02/12/2013 :  06:59:48  Show Profile  Reply with Quote
My original query didn't work in the 2000 DB but did in the 2008 instance.

Also, the code that visakh16 kindly posted works in 2008 but not in 2000. As in 2000 I get the following error:

server: msg 156, level 15, state 1, line 4
incorrect syntax near the keyword 'over'

What is going on here?? dont get it and its annoying!!

Edited by - mitin on 02/12/2013 07:00:56
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22742 Posts

Posted - 02/12/2013 :  07:46:33  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by mitin

My original query didn't work in the 2000 DB but did in the 2008 instance.

Also, the code that visakh16 kindly posted works in 2008 but not in 2000. As in 2000 I get the following error:

server: msg 156, level 15, state 1, line 4
incorrect syntax near the keyword 'over'

What is going on here?? dont get it and its annoying!!


The new OVER clause with SUM will work from 2008 onwards

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mitin
Yak Posting Veteran

81 Posts

Posted - 02/12/2013 :  07:52:28  Show Profile  Reply with Quote
How could the query be altered so that it worked for 2000? ca anyone help this is pretty urgent...

thanks very much!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 02/12/2013 :  10:14:30  Show Profile  Reply with Quote

select 
servername,
firstoccurrence,
lastoccurrence,
eventid,
key,
group,
node,
summary
sum(tally) as NumberofEvents,
count(*) as cnt
into #temp
from databasetable
where firstoccurrence >= '2013-02-04' and firstoccurrence < '2013-02-08'
and lastoccurrence >= '2013-02-04' and lastoccurrence < '2013-02-08'

group by
firstoccurrence,
lastoccurrence,
servername,
eventid,
alertkey,
alertgroup,
node,
summary

select t1.*,
t2.daytotal,
t3.daytotalbyserver
from  #temp t1
join (select datediff(dd,0,lastoccurrence) as daynumber,sum(cnt) as daytotal
      from #temp
      group by datediff(dd,0,lastoccurrence)
     )t2
on t2.daynumber = datediff(dd,0,t1.lastoccurrence)
join (select servername,datediff(dd,0,lastoccurrence) as daynumber,sum(cnt) as daytotalbyserver
      from #temp
      group by servername,datediff(dd,0,lastoccurrence)
     )t3
on t3.servername = t1.servername
and t3.daynumber = datediff(dd,0,t1.lastoccurrence) 

drop table #temp


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 02/12/2013 10:15:05
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000