Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 12th, 2004, 12:24 AM
Registered User
 
Join Date: May 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Group By query with count problem

Hi,
Having trouble getting the results I need with this query.
Basically have two tables, content and contentheaders.
The contentheaders table lists the headings for various menu items, such as White Wine, Red Wine, Champagne etc.
The content table contains the actual menu items that fall under the various headings, such as chardonnay, pinot blanc, riesling under the White Wine heading, and so on.

I am trying to use a select query to display all the headings for the wine webpage, with a count of the number of menu items in the content table for each heading.

The query I have is:

SELECT contentheaders.headername, contentheaders.headerID,
COUNT(content.contentID) As total From contentheaders, content Where contentheaders.contentarea = '$strID' And contentheaders.headerID = content.header Group By contentheaders.headername, contentheaders.headerID

At the moment the query doesn't display any results at all, since there aren't any menu items for those headings yet, but I would still like the query to display the heading names, with a count of zero next to each one, and of course with an accurate count once the content table is populated.

This is the end result I am looking for:

Header Names Number of items
White wine 0
Red Wine 0
Champagne 0


Instead it is returning nothing.

Any help would be greatly appreciated.
Thanks,

Paul
 
Old May 12th, 2004, 12:40 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

When you say it's returing nothing, do you mean nothing or an error?

Am I mistaken or are you asking 'two tables' for a record set?

If so you need a join, unless the answer to the above question is no - I would do something like: (note primaryKeyName and forignKeyName are to replaced with relavent names)

SELECT COUNT(content.contentID) As total, contentheaders.headername,
contentheaders.headerID From contentheaders
INNER JOIN content ON contentheaders.primaryKeyName = content.forignKeyName
Where contentheaders.contentarea = '$strID'
And contentheaders.headerID = content.header
Group By contentheaders.headername, contentheaders.headerID

If you want to dispaly 0 instead of a NULL value you can
do until recordSetName.EoF
   if recordSetName(somenumber) == NULL then
      response.write 0
   else
      response.write recordSetName(somenumber)
   end if
   recordSetName.movenext
loop


Wind is your friend
Matt
 
Old May 12th, 2004, 09:27 AM
Registered User
 
Join Date: May 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help - when I say it returns nothing, there are zero records returned. Asking two tables for a recordset.

Tried the query you wrote but it just resulted in the same, zero records returned. I need an outer join, somthing that returns all the records from the contentheaders table (although just the headers that match the content area specified in the querystring) and then a count of the corresponding records in the content table, zero if there aren't any.

Thanks again.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterate count within for-each-group stolte XSLT 1 November 19th, 2007 07:40 PM
Problem with Count() function 4 group of records Odeh Naber Access 0 July 24th, 2007 03:28 AM
Group by Count * >1? Raith SQL Language 7 May 24th, 2007 03:50 PM
Query Problem group by mateenmohd SQL Server 2000 4 February 4th, 2004 02:44 AM
Help! - How do i count and group this gfowajuh SQL Server 2000 4 August 19th, 2003 05:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.