Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Posted on 2009-07-14
10
Medium Priority
?
532 Views
Last Modified: 2012-05-07
The following SQL throws the error
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

How can I fix it?


select (select sum((select countervalue from alltransaction where alltranid=t.id)) from alltransaction sub_t 
inner join client c on t.clientid = c.id inner join transactiontype r on t.transactiontypeid = r.id 
inner join account a on t.accountid = a.id where alltranid = 0 and t.clientid = '153' and trandatetime >= '2009-07-14' 
and trandatetime < '2009-07-15' and transactiontypeid in (1,2,3,4) and sub_t.currency <= t.currency 
and sub_t.transactiontypeid <= t.transactiontypeid and sub_t.id <= t.id) as Balance from alltransaction t
inner join client c on t.clientid = c.id 
inner join currency u on t.currency = u.currency inner join transactiontype r on t.transactiontypeid = r.id 
inner join account a on t.accountid = a.id where alltranid = 0 and t.clientid = '153' and trandatetime >= '2009-07-14' 
and trandatetime < '2009-07-15' and transactiontypeid in (1,2,3,4) order by [transactiontypeid] asc

Open in new window

0
Comment
Question by:techques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 7

Expert Comment

by:Mohed Sharfi
ID: 24850824
Hi techques,
How are you, please try to use TOP 1
thanks
select top 1(select sum((select countervalue from alltransaction where alltranid=t.id)) from alltransaction sub_t 
inner join client c on t.clientid = c.id inner join transactiontype r on t.transactiontypeid = r.id 
inner join account a on t.accountid = a.id where alltranid = 0 and t.clientid = '153' and trandatetime >= '2009-07-14' 
and trandatetime < '2009-07-15' and transactiontypeid in (1,2,3,4) and sub_t.currency <= t.currency 
and sub_t.transactiontypeid <= t.transactiontypeid and sub_t.id <= t.id) as Balance from alltransaction t
inner join client c on t.clientid = c.id 
inner join currency u on t.currency = u.currency inner join transactiontype r on t.transactiontypeid = r.id 
inner join account a on t.accountid = a.id where alltranid = 0 and t.clientid = '153' and trandatetime >= '2009-07-14' 
and trandatetime < '2009-07-15' and transactiontypeid in (1,2,3,4) order by [transactiontypeid] asc

Open in new window

0
 
LVL 7

Expert Comment

by:Mohed Sharfi
ID: 24850856
Hi ,
you also try select top 100 percent if you need to result all data
thanks
0
 

Author Comment

by:techques
ID: 24850876
Still the same error

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

0
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 
LVL 7

Expert Comment

by:Mohed Sharfi
ID: 24851218
Ok Try this
SELECT TOP 100 PERCENT (SELECT ( SELECT SUM(countervalue) from alltransaction where alltranid=t.id) from alltransaction sub_t  
inner join client c on t.clientid = c.id inner join transactiontype r on t.transactiontypeid = r.id 
inner join account a on t.accountid = a.id where alltranid = 0 and t.clientid = '153' and trandatetime >= '2009-07-14' 
and trandatetime < '2009-07-15' and transactiontypeid in (1,2,3,4) and sub_t.currency <= t.currency 
and sub_t.transactiontypeid <= t.transactiontypeid and sub_t.id <= t.id) as Balance from alltransaction t
inner join client c on t.clientid = c.id 
inner join currency u on t.currency = u.currency inner join transactiontype r on t.transactiontypeid = r.id 
inner join account a on t.accountid = a.id where alltranid = 0 and t.clientid = '153' and trandatetime >= '2009-07-14' 
and trandatetime < '2009-07-15' and transactiontypeid in (1,2,3,4) order by [transactiontypeid] asc

Open in new window

0
 

Author Comment

by:techques
ID: 24851297
A new error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
0
 
LVL 7

Expert Comment

by:Mohed Sharfi
ID: 24851519
Hi,
this with referencing the object name in sub query, please try it & feed back
thanks
SELECT TOP 100 PERCENT (SELECT ( SELECT SUM(alltransaction.countervalue) from alltransaction where alltransaction.alltranid=t.id) from alltransaction sub_t  
inner join client c on t.clientid = c.id inner join transactiontype r on t.transactiontypeid = r.id 
inner join account a on t.accountid = a.id where alltranid = 0 and t.clientid = '153' and trandatetime >= '2009-07-14' 
and trandatetime < '2009-07-15' and transactiontypeid in (1,2,3,4) and sub_t.currency <= t.currency 
and sub_t.transactiontypeid <= t.transactiontypeid and sub_t.id <= t.id) as Balance from alltransaction t
inner join client c on t.clientid = c.id 
inner join currency u on t.currency = u.currency inner join transactiontype r on t.transactiontypeid = r.id 
inner join account a on t.accountid = a.id where alltranid = 0 and t.clientid = '153' and trandatetime >= '2009-07-14' 
and trandatetime < '2009-07-15' and transactiontypeid in (1,2,3,4) order by [transactiontypeid] asc

Open in new window

0
 

Author Comment

by:techques
ID: 24855661
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
0
 
LVL 7

Expert Comment

by:Mohed Sharfi
ID: 24856893
Hi ,
try to use group by in sub query to get on row result, that depend on your data.
thanks
0
 

Author Comment

by:techques
ID: 24858455
I am sorry I do you understand what do you mean 'use group by in sub query to get on row result' as it is just a sum of a select query and do not has group by.

0
 
LVL 7

Accepted Solution

by:
Mohed Sharfi earned 150 total points
ID: 24859176
Hi,
yes I Mean you test only sub query and see the result, it must be return one value.
thanks
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question