Solved

Display data based on a sql count statement

Posted on 2013-05-22
5
371 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
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:mds-cos
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Thanks. The solution to add the fields to the group by making adding any extra fields real easy!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

11 Experts available now in Live!

Get 1:1 Help Now