SQL Query using COUNT
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Web Servers reviews > IIS server support > IIS ASP > SQL Query using COUNT




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    SQL Query using COUNT  
Mark


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-29-04 10:55 PM

Hi, I have a Access database with two tables; a category table and an images
table. In the Category table I have a field for Index number and a field for
Categories. The images table has a field for all the images and a field to
specify what category the images are assigned to.

I am trying to write a simple Select query that will return a list of
Categories from my Category table, and in the second colum a count of the
number of images assigned to each category from my main table of images.

Can anybody suggest how I could do this?

Thanks for any help you can give me,

Mark.







[ Post a follow-up to this message ]



    Re: SQL Query using COUNT  
Aaron [SQL Server MVP]


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-29-04 10:55 PM

SELECT Categories.Name,
COUNT(Images.CategoryID)
FROM Categories INNER JOIN Images
ON Images.CategoryID = Categories.CategoryID
GROUP BY Categories.Name

--
http://www.aspfaq.com/
(Reverse address to reply.)




"Mark" <m@notmyemailaddress.com> wrote in message
news:RmlEc.850$Fc7.199326@stones.force9.net...
> Hi, I have a Access database with two tables; a category table and an
images
> table. In the Category table I have a field for Index number and a field
for
> Categories. The images table has a field for all the images and a field to
> specify what category the images are assigned to.
>
> I am trying to write a simple Select query that will return a list of
> Categories from my Category table, and in the second colum a count of the
> number of images assigned to each category from my main table of images.
>
> Can anybody suggest how I could do this?
>
> Thanks for any help you can give me,
>
> Mark.
>
>







[ Post a follow-up to this message ]



    Re: SQL Query using COUNT  
Bob Barrows [MVP]


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-29-04 10:55 PM

Mark wrote:
> Hi, I have a Access database with two tables; a category table and an
> images table. In the Category table I have a field for Index number
> and a field for Categories. The images table has a field for all the
> images and a field to specify what category the images are assigned
> to.
>
> I am trying to write a simple Select query that will return a list of
> Categories from my Category table, and in the second colum a count of
> the number of images assigned to each category from my main table of
> images.
>
> Can anybody suggest how I could do this?
>
> Thanks for any help you can give me,
>
> Mark.

SELECT c.Categories, count(*)
FROM Category c inner join Images i
ON c.Categories = i.Categories
GROUP BY c.Categories

HTH,
Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"







[ Post a follow-up to this message ]



    Re: SQL Query using COUNT  
Mark


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-29-04 10:55 PM

Thanks Aaron and Bob, its working now!

"Mark" <m@notmyemailaddress.com> wrote in message
news:RmlEc.850$Fc7.199326@stones.force9.net...
> Hi, I have a Access database with two tables; a category table and an
images
> table. In the Category table I have a field for Index number and a field
for
> Categories. The images table has a field for all the images and a field to
> specify what category the images are assigned to.
>
> I am trying to write a simple Select query that will return a list of
> Categories from my Category table, and in the second colum a count of the
> number of images assigned to each category from my main table of images.
>
> Can anybody suggest how I could do this?
>
> Thanks for any help you can give me,
>
> Mark.
>
>







[ Post a follow-up to this message ]



    Re: SQL Query using COUNT  
Mark


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-30-04 08:35 AM

Hi, I have one other quick question: if I wanted to include the ID field for
each category, would it just be a case of changing the SQL from:

SELECT Categories.Name,
COUNT(Images.CategoryID)
FROM Categories INNER JOIN Images
ON Images.CategoryID = Categories.CategoryID
GROUP BY Categories.Name

TO:

SELECT Categories.ID, Categories.Name,
COUNT(Images.CategoryID)
FROM Categories INNER JOIN Images
ON Images.CategoryID = Categories.CategoryID
GROUP BY Categories.Name

So far this has not worked and I get the error:

"You tried to execute a query that does not include the specified expression
'ID' as part of an aggregate function"



"Mark" <m@notmyemailaddress.com> wrote in message
news:RmlEc.850$Fc7.199326@stones.force9.net...
> Hi, I have a Access database with two tables; a category table and an
images
> table. In the Category table I have a field for Index number and a field
for
> Categories. The images table has a field for all the images and a field to
> specify what category the images are assigned to.
>
> I am trying to write a simple Select query that will return a list of
> Categories from my Category table, and in the second colum a count of the
> number of images assigned to each category from my main table of images.
>
> Can anybody suggest how I could do this?
>
> Thanks for any help you can give me,
>
> Mark.
>
>







[ Post a follow-up to this message ]



    Re: SQL Query using COUNT  
Mark


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-30-04 08:35 AM

Sorted it with this:

SELECT tblCategories.ID, tblCategories.CatName,
COUNT(tblImages.CategoryID)
FROM tblCategories INNER JOIN tblImages
ON tblImages.CategoryID = tblCategories.ID
GROUP BY tblCategories.CatName, tblCategories.ID

Thanks.....
"Mark" <m@notmyemailaddress.com> wrote in message
news:RmlEc.850$Fc7.199326@stones.force9.net...
> Hi, I have a Access database with two tables; a category table and an
images
> table. In the Category table I have a field for Index number and a field
for
> Categories. The images table has a field for all the images and a field to
> specify what category the images are assigned to.
>
> I am trying to write a simple Select query that will return a list of
> Categories from my Category table, and in the second colum a count of the
> number of images assigned to each category from my main table of images.
>
> Can anybody suggest how I could do this?
>
> Thanks for any help you can give me,
>
> Mark.
>
>







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:27 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register