Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Counting distinct values in one column of a view

Posted on 2007-11-30
5
Medium Priority
?
1,929 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 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 2000 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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

963 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