Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Counting Query

Posted on 2002-04-12
7
Medium Priority
?
217 Views
Last Modified: 2013-12-24
I'm working on a web based IT knowledbase type app for my personal use. I'm trying to accomplish something I have seen on other websites. I have a DB with two tables. One table has all the different categories and their category id, and the other table has a the actual content, category ID, etc. (Look at the attached code below and look at the CFQUERY pulling out of the KNOWLEDGEBASE table or download a copy of this DB here: http://www.georgelemos.com/glkb.zip)

If you go to http://www.georgelemos.com/categories.cfm you will see the template I am working on. You'll notice the (4) next to each category. What I want to do, is query the KNOWLEDGEBASE table and count how many articles I have stored in the DB for each CATEGORY. Then next to each category have a number displaying how many articles for that CATEGORY are stored in the DB.

I have tried a few different ways but I really came up with nothing that works. Right now all its doing is displaying how many entries are in cetegory 1 (which are 4) I am assuming there's some type of CFLOOP needed which I am really awful with.

If someone has a piece of code available that could help me out I would greatly appreciate it. The code below is what I have running on the page URL listed above.

Thanks!

<CFQUERY NAME="CATZ" DATASOURCE="GLKB">
   Select CATID, CATEGORY
   From CATEGORIES
   Order By CATEGORY
</CFQUERY>



<CFQUERY NAME="NUMZ" DATASOURCE="GLKB">
  SELECT CATEGORIES.CATEGORY, CATEGORIES.CATID, (SELECT Count(*)
  FROM KNOWLEDGEBASE where KNOWLEDGEBASE.CATEGORY = CATEGORIES.CATID) AS CategoryCount
  FROM CATEGORIES;
</CFQUERY>









<HTML>
<HEAD>
 <TITLE>Categories</TITLE>
 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
 <LINK REL="stylesheet" HREF="glcss.css" TYPE="TEXT/CSS">
</HEAD>






<TABLE WIDTH="525" CELLSPACING="3" CELLPADDING="3">
<TR>                                          
<CFOUTPUT QUERY="CATZ">
<TD><A HREF="catpage.cfm?CATID=#CATZ.CATID#">#CATZ.CATEGORY#</A> (#NUMZ.CategoryCount#)<BR>
</TD>
<CFIF CATZ.CURRENTROW MOD 2 IS 0>
</TR>
<TR>
</CFIF>
</CFOUTPUT>
</TABLE>


</BODY>
</HTML>
0
Comment
Question by:gplemos
[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
  • 2
7 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 6936900
<CFQUERY NAME="CATZ" DATASOURCE="GLKB">
  Select CATID, CATEGORY
  From CATEGORIES
  Order By CATEGORY
</CFQUERY>

<cfloop query="CATZ">
<CFQUERY NAME="NUMZ" DATASOURCE="GLKB">
 SELECT CATEGORIES.CATEGORY, CATEGORIES.CATID, COUNT(*) AS CategoryCount
FROM KNOWLEDGEBASE, CATEGORIES where KNOWLEDGEBASE.CATEGORY = CATZ.CATID
AND CATEGORIES.CATID = KNOWLEDGEBASE.CATEGORY
</CFQUERY>
</cfloop>

should work.

Just use the query numz now to display the stuff like:

<cfoutput query="NUMZ">
#NUMZ.CATEGORY# (#NUMZ.CategoryCount#)<br>

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6936904
oops forgot the ending </cfoutput>:
<cfoutput query="NUMZ">
#NUMZ.CATEGORY# (#NUMZ.CategoryCount#)<br>
</cfoutput>

0
 
LVL 1

Accepted Solution

by:
ndintenfass earned 400 total points
ID: 6937437
Looping over a query, though, can be "expensive" overhead.  You can accomplish this in one query like this:

<CFQUERY NAME="CATZ" DATASOURCE="GLKB">
  Select     CATEGORIES.CATID,
                 CATEGORIES.CATEGORY,
               COUNT(KNOWLEDGEBASE.CATEGORY) As CategoryCount
               
  From           KnowledgeBase RIGHT JOIN CATEGORIES ON KNOWLEDGEBASE.CATEGORY = CATEGORIES.CATID
 
  GROUP BY      CATEGORIES.CATID,CATEGORIES.CATEGORY
 
  Order By      CATEGORIES.CATEGORY
</CFQUERY>

<HTML>
<HEAD>
<TITLE>Categories</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="stylesheet" HREF="glcss.css" TYPE="TEXT/CSS">
</HEAD>

<TABLE WIDTH="525" CELLSPACING="3" CELLPADDING="3">
<TR>                                          
<CFOUTPUT QUERY="CATZ">
<TD><A HREF="catpage.cfm?CATID=#CATZ.CATID#">#CATEGORY#</A> (#CategoryCount#)<BR>
</TD>
<CFIF CATZ.CURRENTROW MOD 2 IS 0>
</TR>
<TR>
</CFIF>
</CFOUTPUT>
</TABLE>


</BODY>
</HTML>


OR, if you want only the categories that do have entries in the knowledge base, you can simply have the query be:


  Select     CATEGORIES.CATID,
                 CATEGORIES.CATEGORY,
               COUNT(KNOWLEDGEBASE.CATEGORY) As CategoryCount
  From           CATEGORIES,
                 KNOWLEDGEBASE
  WHERE          KNOWLEDGEBASE.CATEGORY = CATEGORIES.CATID
  GROUP BY      CATEGORIES.CATID,CATEGORIES.CATEGORY
  Order By      CATEGORIES.CATEGORY
0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 19

Expert Comment

by:cheekycj
ID: 6937474
>Looping over a query, though, can be "expensive" overhead

true but it really does depend on the data set.

If the categories is under like 500 .. its probably
fine performance wise.

CJ
0
 

Author Comment

by:gplemos
ID: 6937520



Using the code below on URL:
http://www.georgelemos.com/categories2.cfm

I get the error:

"ODBC Error Code = 37000 (Syntax error or access violation)
[Microsoft][ODBC Microsoft Access Driver] You tried to execute a query that does not include the specified expression 'CATEGORY' as part of an aggregate function.



The error occurred while processing an element with a general identifier of (CFQUERY), occupying document position (8:1) to (8:39)."


Any suggestions?













<CFQUERY NAME="CATZ" DATASOURCE="GLKB">
 Select CATID, CATEGORY
 From CATEGORIES
 Order By CATEGORY
</CFQUERY>

<cfloop query="CATZ">
<CFQUERY NAME="NUMZ" DATASOURCE="GLKB">
SELECT CATEGORIES.CATEGORY, CATEGORIES.CATID, COUNT(*) AS CategoryCount
FROM KNOWLEDGEBASE, CATEGORIES where KNOWLEDGEBASE.CATEGORY = CATZ.CATID
AND CATEGORIES.CATID = KNOWLEDGEBASE.CATEGORY
</CFQUERY>
</cfloop>







<HTML>
<HEAD>
<TITLE>Categories</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="stylesheet" HREF="glcss.css" TYPE="TEXT/CSS">
</HEAD>






<cfoutput query="NUMZ">
#NUMZ.CATEGORY# (#NUMZ.CategoryCount#)<br>
</cfoutput>



</BODY>
</HTML>
0
 
LVL 1

Expert Comment

by:ndintenfass
ID: 6937757
I think you need a GROUP BY clause in the query (see my comment from above about doing it all in one query -- same idea)
0
 

Author Comment

by:gplemos
ID: 6940381
That did the trick. Thank you to both of you.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

718 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