Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Cannot perform aggregate function on subquery

Author  Topic 

logeshwar
Starting Member

6 Posts

Posted - 2009-09-03 : 13:37:54
as

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-04 : 02:13:47
try


select a.division,
a.subregion,
a.district,
a.territory,

(
select sum(coalesce((case when rp.contractenddate is null then 0 when rp.contractenddate >= w.startdate and rp.contractenddate < w.enddate then 1 else 0 end),0))
from rpRepHomeBaseLink y
join rpt_weeknotodate w
on w.yearweekno = a.weekno
join rpContract rp
on y.contractid = rp.contractid
where y.homebasename = a.territory
and y.startweekno <= a.weekno
and (y.endweekno >= a.weekno
or y.endweekno is null )
)
as terminations


from maSalesOfficeView a

where a.weekno between 200901 and 200926


group by a.division,
a.subregion,
a.district,
a.territory

order by 4

Madhivanan

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

logeshwar
Starting Member

6 Posts

Posted - 2009-09-06 : 08:13:58
I ma recieving the following error after executing the above said query

"Column 'a.WEEKNO' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause".
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-09-06 : 14:06:31
whats the purpose of group by here? wont this be enough?


select a.division,
a.subregion,
a.district,
a.territory,

(
select sum(coalesce((case when rp.contractenddate is null then 0 when rp.contractenddate >= w.startdate and rp.contractenddate < w.enddate then 1 else 0 end),0))
from rpRepHomeBaseLink y
join rpt_weeknotodate w
on w.yearweekno = a.weekno
join rpContract rp
on y.contractid = rp.contractid
where y.homebasename = a.territory
and y.startweekno <= a.weekno
and (y.endweekno >= a.weekno
or y.endweekno is null )
)
as terminations


from maSalesOfficeView a

where a.weekno between 200901 and 200926
order by 4

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-07 : 10:08:25
Can you post the query you used now?

Madhivanan

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

logeshwar
Starting Member

6 Posts

Posted - 2009-09-07 : 11:40:17
Hi The second query which you gave worked fine.The first query which you gave had the group by clause.that is why the error ws thrown....

Thanks a lot
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-05 : 05:32:45
What happened to the original post?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-05 : 06:24:33
quote:
Originally posted by Kristen

What happened to the original post?


OP got answer and dont want to show the actual code

Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-05 : 09:08:33
quote:
Originally posted by logeshwar

Hi The second query which you gave worked fine.The first query which you gave had the group by clause.that is why the error ws thrown....

Thanks a lot


thats fine
but why did you remove your original post. Please dont do this as it might help somebody in future who might have had same problem
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-05 : 10:06:55
Here is the original one
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=132210

I am quoting here
quote:

I have a DB2 query which needs to be converted to be compliant with SQL Server. The challenge is to work around the Aggregate functioning on subqueried which is not allowed in Sql Server.

i run this query and i get the error message as
cannot perform aggregate functions on subqueries..

Can u help me in the workaround

The DB2 Query is

select a.div,
a.subreg,
a.dis,
a.ter,

sum(coalesce(
(
select sum(b.finalcount)
from HomeBaseLink x
join pMW b
on b.level = 2
and b.weekno = a.weekno
and b.repcontractid = x.contractid
where x.homebasename = a.ter
and x.startweekno <= a.weekno
and (x.endweekno > a.weekno
or x.endweekno is null )

)
,0))
as finalcount,


sum(coalesce(
(
select count(distinct z.contractid)
from HBL z
where z.homebasename = a.ter
and z.startweekno <= a.weekno
and (z.endweekno >= a.weekno
or z.endweekno is null )
)
,0))
as accumulatedcount,
sum(coalesce(
(
select sum(coalesce((case when rp.contractenddate is null then 0 when rp.contractenddate >= w.startdate and rp.contractenddate < w.enddate then 1 else 0 end),0))
from HBL y
join rpt_weeknotodate w
on w.yearweekno = a.weekno
join rpContract rp
on y.contractid = rp.contractid
where y.homebasename = a.territory
and y.startweekno <= a.weekno
and (y.endweekno >= a.weekno
or y.endweekno is null )
)
,0)) as terminations


from maSales a

where a.weekno between 200901 and 200926


group by a.div,
a.subreg,
a.dis,
a.ter

order by 4
;


Madhivanan

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

Kristen
Test

22859 Posts

Posted - 2010-02-05 : 10:14:59
Thanks Madhi
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-05 : 10:16:50
quote:
Originally posted by Kristen

Thanks Madhi




I thought OP may delete that too
Thats why I quoted here

Madhivanan

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

- Advertisement -