[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 871
  • Last Modified:

sql DISTINCT

hey guy i need to count the number of DISTINCT

but this statement

SELECT DISTINCT description, COUNT(DISTINCT description) from groupon GROUP BY description

output

description                                                                              Column1    
  d                                                                                                  1

i have d description in my table
0
JCWEBHOST
Asked:
JCWEBHOST
3 Solutions
 
XaelianCommented:
Can you do this:

SELECT count(DISTINCT description) FROM groupon GROUP BY description;
0
 
chaitu chaituCommented:
SELECT description, COUNT(DISTINCT description) from groupon GROUP BY description
0
 
selva_konguCommented:
SELECT COUNT(description) , description FROM GROUPON GROUP BY description
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
QlemoC++ DeveloperCommented:
Besides the first DISTINCT is superfluous (group-by columns are always distinct) I cannot see something wrong. Try
select distinct description from groupon;
select description, count(*), count(description), count(distinct description) from groupon group by description;

Open in new window

to compare the results.
0
 
Ephraim WangoyaCommented:
You dont need to use distinct, you are already grouping with the field
0
 
Ephraim WangoyaCommented:
If you simply need to display the distinct descriptions

select description
from groupon
group by description

or if you want to use distinct

select distinct description
from groupon
0
 
QlemoC++ DeveloperCommented:
Sorry, major failure. Counting the group-by column will always result in 1, because the grouped results contain a single record for each description only. You have to execute:
select description, count(*) from groupon group by description;

Open in new window

which will count all rows per description. If you want to check for a particular column not having NULLs, use that column in the count
0
 
Ephraim WangoyaCommented:
and for the count of distinct descriptions simply use

select description, Count(*) [Count]
from groupon
group by description
0
 
awking00Commented:
I'm not even sure if the question is totally understood at this point. If you have 2 descriptions of 'A', 4 of 'B', and 1 of 'C', do you want to see
Desc Count
A        2
B        4
C        1
i.e. a count for each distinct description, or do you simply want to see
Desc
 3
since there are only 3 distinct descriptions?
There have been multiple solutions to the first case -
select description, count(*) from groupon group by description;
To return results as in the second case, eliminate the group by -
select count(distinct description) as description from groupon;
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now