Solved

Counting distinct values in one column of a view

Posted on 2007-11-30
5
1,925 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

728 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