Solved

Display data based on a sql count statement

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 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