Solved

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

Posted on 2009-07-14
10
520 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
  • 6
  • 4
10 Comments
 
LVL 7

Expert Comment

by:Mohed Sharfi
Comment Utility
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
Comment Utility
Hi ,
you also try select top 100 percent if you need to result all data
thanks
0
 

Author Comment

by:techques
Comment Utility
Still the same error

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

0
 
LVL 7

Expert Comment

by:Mohed Sharfi
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Expert Comment

by:Mohed Sharfi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 50 total points
Comment Utility
Hi,
yes I Mean you test only sub query and see the result, it must be return one value.
thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now