Solved

SQL Count and Group By

Posted on 2013-06-17
2
637 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now