dbTalk Databases Forums  

Subquery

comp.databases.ms-access comp.databases.ms-access


Discuss Subquery in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
WhathaveIdone?
 
Posts: n/a

Default Subquery - 02-01-2012 , 03:17 PM






I posted this question once before without a clear answer. I would
appreciate any advice I could get.
I am having a problem running a sub query in VBA. I trying to build a
where clause that will be inserted when I open the report.
I am getting a syntax error when I run it in VBA but not when I run it
in a query. Here is the problematic portion of my WHERE clause code:

(EXISTS(SELECT Sales.AgentID,Sum(Sales.Premium), AS SumOfPremium FROM
Sales Group BY Sales.AgentID HAVING (((Sum(Sales.Premium)) > =
500000))<>False))

In words this is supposed to pull the data where the sum of premium
for each agentID >= 500000. What syntax am I missing because I don't
seem to catch the error when it works in a query just fine. Thanks in
advance.

-WhathaveIdone?

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Subquery - 02-01-2012 , 04:13 PM






WhathaveIdone? wrote:
Quote:
I posted this question once before without a clear answer. I would
appreciate any advice I could get.
I am having a problem running a sub query in VBA. I trying to build a
where clause that will be inserted when I open the report.
I am getting a syntax error when I run it in VBA but not when I run it
in a query. Here is the problematic portion of my WHERE clause code:

(EXISTS(SELECT Sales.AgentID,Sum(Sales.Premium), AS SumOfPremium FROM
Sales Group BY Sales.AgentID HAVING (((Sum(Sales.Premium)) > =
500000))<>False))

In words this is supposed to pull the data where the sum of premium
for each agentID >= 500000. What syntax am I missing because I don't
seem to catch the error when it works in a query just fine. Thanks in
advance.

-WhathaveIdone?
Two things:
- the lack of a space between EXISTS and the opening parenthesis is
worrying, but might not be a problem. I would tuck a space in there just to
be sure
- the comma between Sum(Sales.Premium) and AS SumOfPremium will definitely
throw an error
Oh! I just saw a third problem: the space between > and = in that HAVING
clause

My personal preference is to eliminate as many parentheses from sql in VBA
code as possible to make it easier to find those syntax errors. The Query
Builder is great, but it really overdoes the parentheses. Also, with only a
single table in the FROM clause, there is no need to qualify the field
names - there is no ambiguity.

EXISTS (SELECT AgentID,Sum(Premium) AS SumOfPremium FROM
Sales Group BY AgentID HAVING (Sum(Premium) >= 500000)<>False)

And you can get rid of that <>False part, leaving:

EXISTS (SELECT AgentID,Sum(Premium) AS SumOfPremium FROM
Sales Group BY AgentID HAVING Sum(Premium) >= 500000)

This can be simplified even further. There is no need to have the
aggregation in the SELECT clause - you're not returning that to the main
query. And I think you don't even need the AgentID in the SELECT. This will
work:

EXISTS (SELECT * FROM Sales Group BY AgentID HAVING Sum(Premium) >= 500000)

A little easier to read, don't you think?

Reply With Quote
  #3  
Old   
Ron Paii
 
Posts: n/a

Default Re: Subquery - 02-02-2012 , 07:13 AM



"Bob Barrows" <reb01501 (AT) NOyahooSPAM (DOT) com> wrote

Quote:
WhathaveIdone? wrote:
I posted this question once before without a clear answer. I would
appreciate any advice I could get.
I am having a problem running a sub query in VBA. I trying to build a
where clause that will be inserted when I open the report.
I am getting a syntax error when I run it in VBA but not when I run it
in a query. Here is the problematic portion of my WHERE clause code:

(EXISTS(SELECT Sales.AgentID,Sum(Sales.Premium), AS SumOfPremium FROM
Sales Group BY Sales.AgentID HAVING (((Sum(Sales.Premium)) > =
500000))<>False))

In words this is supposed to pull the data where the sum of premium
for each agentID >= 500000. What syntax am I missing because I don't
seem to catch the error when it works in a query just fine. Thanks in
advance.

-WhathaveIdone?

Two things:
- the lack of a space between EXISTS and the opening parenthesis is
worrying, but might not be a problem. I would tuck a space in there just
to
be sure
- the comma between Sum(Sales.Premium) and AS SumOfPremium will definitely
throw an error
Oh! I just saw a third problem: the space between > and = in that HAVING
clause

My personal preference is to eliminate as many parentheses from sql in VBA
code as possible to make it easier to find those syntax errors. The Query
Builder is great, but it really overdoes the parentheses. Also, with only
a
single table in the FROM clause, there is no need to qualify the field
names - there is no ambiguity.

EXISTS (SELECT AgentID,Sum(Premium) AS SumOfPremium FROM
Sales Group BY AgentID HAVING (Sum(Premium) >= 500000)<>False)

And you can get rid of that <>False part, leaving:

EXISTS (SELECT AgentID,Sum(Premium) AS SumOfPremium FROM
Sales Group BY AgentID HAVING Sum(Premium) >= 500000)

This can be simplified even further. There is no need to have the
aggregation in the SELECT clause - you're not returning that to the main
query. And I think you don't even need the AgentID in the SELECT. This
will
work:

EXISTS (SELECT * FROM Sales Group BY AgentID HAVING Sum(Premium) >=
500000)

A little easier to read, don't you think?


Good information but I don't think * will work in Jet. I tried it on one of
my tables and got the following errors (Cannot group on fields selected with
'*').

Reply With Quote
  #4  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Subquery - 02-02-2012 , 06:25 PM



Ron Paii wrote:
Quote:
This can be simplified even further. There is no need to have the
aggregation in the SELECT clause - you're not returning that to the
main query. And I think you don't even need the AgentID in the
SELECT. This will
work:

EXISTS (SELECT * FROM Sales Group BY AgentID HAVING Sum(Premium) >=
500000)

A little easier to read, don't you think?


Good information but I don't think * will work in Jet. I tried it on
one of my tables and got the following errors (Cannot group on fields
selected with '*').
Yes, you're correct of course. The grouped by field needs to be in the
SELECT ... but nothing else.

EXISTS (SELECT AgentID FROM Sales Group BY AgentID HAVING Sum(Premium) >=
500000)

Without the GROUP BY, "SELECT *" would be correct.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.