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
Solved

SQL Count and Group By

Posted on 2013-06-17
2
642 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

840 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