Solved

Counting distinct values in one column of a view

Posted on 2007-11-30
5
1,919 Views
Last Modified: 2010-04-21
I have a database view that joins several tables together.  I am now selecting a recordset from this view, and I want to count the distinct number of one of the values in the result set generated by this view.  For this question I suppose the view can simply be treated as a single table -- the fact that it's a view is really inconsequential.  The query as I've attempted it is:

SELECT     *, COUNT(DISTINCT questionPK) AS questionTotal
FROM         PlenarySurvey
WHERE     (surveyPK = 1) AND (pageNumber = 2)
ORDER BY questionSequence, choiceSequence

But this generates the dreaded

"Column 'abc' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."

I then tried adding all of the columns in the view to the GROUP BY clause, but then I got a different error, about text, ntext etc. not being valid in the group by clause (plus, that just doesn't "feel" like it would be the right answer anyway...)

How can I select all columns of the view (table), but also count distinct values of one of the view's (table's) columns, without screwing around with GROUP BY?
0
Comment
Question by:funaroma
  • 3
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20382055
you will need something like this... not sure if you need/want the where condition also there...

SELECT     *, ( select COUNT(DISTINCT questionPK) from PlenarySurvey WHERE     (surveyPK = 1) AND (pageNumber = 2) ) AS questionTotal
FROM         PlenarySurvey
WHERE     (surveyPK = 1) AND (pageNumber = 2)
ORDER BY questionSequence, choiceSequence
0
 

Author Comment

by:funaroma
ID: 20382167
I stumbled upon a different method that gives me the same result:

SELECT     PlenarySurvey.*, qc.questionTotal
FROM         PlenarySurvey INNER JOIN
                          (SELECT     pagePK, COUNT(DISTINCT questionPK) AS questionTotal
                            FROM          PlenarySurvey AS PlenarySurvey_1
                            GROUP BY pagePK) AS qc ON PlenarySurvey.pagePK = qc.pagePK
WHERE     (PlenarySurvey.surveyPK = 1) AND (PlenarySurvey.pageNumber = 2)
ORDER BY PlenarySurvey.questionSequence, PlenarySurvey.choiceSequence

So now the nature of my question changes a bit -- WHICH of these two choices is more efficient for the SQL Server to process?  My solution above joins two tables but only requires passing in the surveyPk and pageNumber in one place
0
 

Author Comment

by:funaroma
ID: 20382199
and further, would it be even more efficient to include the COUNT (DISTINCT) clause in the VIEW, rather than essentially querying the view twice?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20382246
SELECT     ps.*
, ( select COUNT(DISTINCT i.questionPK)
    from PlenarySurvey i
   WHERE     i.surveyPK = ps.surveyPK
      AND  i.pageNumber = ps.pageNumber
 ) AS questionTotal
FROM         PlenarySurvey ps
WHERE     ps.surveyPK = 1
AND ps.pageNumber = 2
ORDER BY ps.questionSequence, ps.choiceSequence
0
 

Author Closing Comment

by:funaroma
ID: 31411908
Fast and concise... thank you!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

757 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

20 Experts available now in Live!

Get 1:1 Help Now