Solved

Display data based on a sql count statement

Posted on 2013-05-22
5
373 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
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

17 Experts available now in Live!

Get 1:1 Help Now