Solved

Counting distinct values in one column of a view

Posted on 2007-11-30
5
1,923 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
[X]
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
  • 3
  • 2
5 Comments
 
LVL 143

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 143

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
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 setup several different housekeeping processes for a SQL Server.

756 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