Solved

SQL Count and Group By

Posted on 2013-06-17
2
640 Views
Last Modified: 2013-06-17
I am connecting to an MS Access database. I want to count the occurrences of responses, including records with NO response. My SQL statement is counting and providing results. The problem is, it is displaying 2 results for records with NO RESPONSE.

My SQL statement is:

sqlTitle = "select iif(len(LibManTitle)=0 or LibManTitle is null,'No Response',LibManTitle) as LibManTitle_Title, count(*) as LibManTitleCount " & _
      "FROM tblStaff " & _
      "GROUP BY LibManTitle "

      Set objTitle = Server.CreateObject("ADODB.Recordset")
      objTitle.Open sqlTitle, objConn


My display is:

No Response: 8
No Response: 24
Administrative: 18

My code to display results:

while not objTitle.eof
      Response.Write "<ul>"
      Response.Write "<li>" & objTitle("LibManTitle_title") & ": " & objTitle("LibManTitleCount") & "</li>"
      objTitle.movenext
      Response.Write "</ul>"
wend
0
Comment
Question by:Malloy1446
2 Comments
 
LVL 6

Accepted Solution

by:
ianmills2002 earned 500 total points
ID: 39255090
You have to put iif(len(LibManTitle)=0 or LibManTitle is null,'No Response',LibManTitle) in the Group by clause

i.e.

sqlTitle = "select iif(len(LibManTitle)=0 or LibManTitle is null,'No Response',LibManTitle) as LibManTitle_Title, count(*) as LibManTitleCount " & _
      "FROM tblStaff " & _
      "GROUP BY iif(len(LibManTitle)=0 or LibManTitle is null,'No Response',LibManTitle) "
0
 

Author Closing Comment

by:Malloy1446
ID: 39255096
THanks for the quick response. TO make the No response appear at the top of the list, I just added a space in front of the ' No Response'.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

785 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