?
Solved

Display data based on a sql count statement

Posted on 2013-05-22
5
Medium Priority
?
381 Views
Last Modified: 2013-05-23
I have a SQL statement counting the number of records based on certain criteria.

My SQL statement is:


      sqlCountChartID ="SELECT tblVirtualRef.ChartID, tblGeneral.City, Count(tblVirtualRef.VRID) AS CountChartID " & _
    "FROM tblVirtualRef INNER JOIN tblGeneral ON tblGeneral.ChartID= tblVirtualRef.ChartID " & _
      "WHERE tblVirtualRef.VRNotEligible LIKE 'Y' " & _
    "GROUP BY tblGeneral.City, tblVirtualRef.ChartID;"

      Set objCountChartID = Server.CreateObject("ADODB.Recordset")
      objCountChartID.Open sqlCountChartID, objConn

When I add tblGeneral.State to the SQL statement (right after tblGeneral.City) I get the following error.

         Microsoft JET Database Engine error '80040e21'
         You tried to execute a query that does not include the specified expression 'State' as part of an aggregate function.
0
Comment
Question by:Malloy1446
[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
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:mds-cos
ID: 39188900
The easy way is to put tblGeneral.State into your GROUP BY clause...assuming your data set is such that this will not affect your count values. (yes, I understand you are not trying to group by State).  So you would have:

...GROUP BY tblGeneral.City, tblVirtualRef.ChartID, tblGeneral.State

The more difficult approach, and arguably better, is to use a temp table to isolate what you actually want to group by from what you only want to select.  This will be necessary if your data set will cause count values to change with the added group by.  So the approach would be:

SELECT t1.ChartID, t1.City, t1.CountChartID, tblGeneral.State FROM
tblGeneral INNER JOIN
(SELECT tblVirtualRef.ChartID, tblGeneral.City, Count(tblVirtualRef.VRID) AS CountChartID
FROM tblVirtualRef INNER JOIN tblGeneral ON tblGeneral.ChartID= tblVirtualRef.ChartID WHERE tblVirtualRef.VRNotEligible LIKE 'Y'
GROUP BY tblGeneral.City, tblVirtualRef.ChartID) t1


My brain is mush right now, so I hope I didn't flubber the syntax.
0
 

Author Comment

by:Malloy1446
ID: 39189015
I have never use two FROM statements in the same SQL clause. I have tried to add the following, but cannot get the right syntax...

      sqlCountChartID ="SELECT tblGeneral.ChartID, t1.City, tblGeneral.CountChartID, tblGeneral.State " & _
      "FROM tblGeneral INNER JOIN (SELECT tblVirtualRef.ChartID, tblGeneral.City, Count(tblVirtualRef.VRID) AS CountChartID " & _
      "FROM tblVirtualRef INNER JOIN tblGeneral ON tblGeneral.ChartID= tblVirtualRef.ChartID WHERE tblVirtualRef.VRNotEligible LIKE 'Y' " & _
      "GROUP BY tblGeneral.City, tblVirtualRef.ChartID)"
      
      Set objCountChartID = Server.CreateObject("ADODB.Recordset")
      objCountChartID.Open sqlCountChartID, objConn

Which generates the "Website cannot display the page" error
0
 
LVL 14

Expert Comment

by:mds-cos
ID: 39189129
You are actually not using two FROM statements in the same query (that would be illegal).  You are creating a temporary table (t1) out of your aggregate SELECT statement.  You are then joining the results of that temporary table in your main query to pull all of the fields you want.

And I apologize for my brain right now.  Just back from a late flight and little sleep...I totally left out the ON condition for join statement to the temp table.  Duh...!!!!

...) t1 ON tblGeneral.ChartID = t1.ChartID
0
 
LVL 14

Accepted Solution

by:
mds-cos earned 2000 total points
ID: 39189227
Oh man...scratch that all....I must be on drugs.  I'm trying to give you a solution to the wrong problem and also bringing in some theoretical arguments about how a GROUP BY should be used that really has little bearing on your question.  Sorry so confusing.

So hear is the deal.  You are selecting the count of records...thus an aggregate.  If you want to add in tblGeneral.State then all you have to do is put tblGeneral.State into your GROUP BY as I suggested.  Microsoft is picky about aggregates, demanding that every field selected outside an aggregate function (like COUNT) be included somewhere in the grouping.  So your entire VBA text string that you are building for the query would be:

sqlCountChartID ="SELECT tblVirtualRef.ChartID, tblGeneral.City, tblGeneral.State, Count(tblVirtualRef.VRID) AS CountChartID " & _
    "FROM tblVirtualRef INNER JOIN tblGeneral ON tblGeneral.ChartID= tblVirtualRef.ChartID " & _
      "WHERE tblVirtualRef.VRNotEligible LIKE 'Y' " & _
    "GROUP BY tblGeneral.City,  tblGeneral.State, tblVirtualRef.ChartID;"


This will of course change your count value if your data set has unique City / State pairings (Paris, TX, Paris, CA).  That may be what you want though.
0
 

Author Closing Comment

by:Malloy1446
ID: 39192008
Thanks. The solution to add the fields to the group by making adding any extra fields real easy!
0

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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