Link to home
Start Free TrialLog in
Avatar of blackcatkempo
blackcatkempo

asked on

SQL Query Assistance (Disinct Count)

Hello, I am looking for assistance creating a query.  I need to count the number of distinct records based on the name of the record and rated by category.  See sample below.

SELECT DISTINCT COUNT(*) AS Count, [Category]
FROM [table name]
WHERE NAME LIKE [Name]
GROUP BY [Category]

I want to list the following columns
Count| Name| Category

Thank you
Avatar of Phillip Burton
Phillip Burton

It might help if you give some sample data and sample expected output,
Avatar of blackcatkempo

ASKER

Attached is sample data.  I need to sort through thousands of records similar.  Key columns Category and Name.  I want a count of a distinct name based on category.
Nothing attached.
Avatar of Scott Pletcher
COUNT can use DISTINCT inside it, which is really useful:

SELECT COUNT(DISTINCT Name) AS Count_Distinct_Names, [Category]
FROM [table name]
WHERE NAME LIKE [Name] --??
GROUP BY [Category]
I think I need to clarify.  Using the sample-data.csv.  I would like to report the total number of distinct records (name) that meet a specific criteria (category).  

SELECT DISTINCT COUNT(*) AS Count, Risk
FROM [tablename]
WHERE NAME LIKE 'Microsoft XML Parser%'
GROUP BY Category
ORDER BY Category ASC

Results in (columns in pipes) ---- I want another column with the name, which is related/assigned a category.
Count|Category
32| High

Need to result with following columns.

Count of Distinct Name | Name | Category

Please let me know if I need to provide further detail.  Thank you.
Without a sample data set leading to a sample result, I cannot help you.

Your attached sample data does not lead to a result of 32, High, and therefore I would still be guessing as to what you want.
Attached is a sanitized version for sample data.  Using this file, I would like to see the following columns in a query displaying the number of distinct occurrences of the name record.

Count of Distinct Name | Name | Risk

Count of distinct name - the number of instances of the name
Name - the name of the risk
Risk - the risk level

Does this help?
Select [Name], Count(Distinct [Name]) as CountOfDistinctName, Risk
From [tablename]
Group by [Name], Risk

From what you have described, the only thing wrong with your previous example was the placement of the word "Distinct".
Note that it is very similar to Scott's solution.
Thank you for the query. I would like to see the number of distinct occurrences (risks) displayed for each name .  The script your provided only displays a "1" for each row (name).  I apologize if i am not explaining properly.
Then

Select [Name], Count(Distinct [Risk]) as CountOfDistinctName, Risk
From [tablename]
Group by [Name], Risk
Sorry that query provided the same result - only displays a "1".  I have confirmed there are more then one instance per name using a separate individual query.
Select [Name],
    (select Count(Distinct [Risk])
    From [tablename] as U
    Where T.[Name] = U.[Name]) as CountOfDistinctName,
Risk
From [tablename] as T
Group by [Name], Risk
Same result.
Try this:

Select [Name], 
    (select Count([Risk]) 
    From [tablename] as U
    Where T.[Name] = U.[Name]) as CountOfDistinctName,
Risk
From [tablename] as T
Group by [Name], Risk

Open in new window

This gives this result:

Adobe Reader Detection      1      None
Adobe Reader Enabled in Browser (Internet Explorer)      1      None
Antivirus Software Check      1      None
BIOS Version (WMI)      1      None
Citrix Online Plug-in Installed      1      None
Citrix Receiver / Online Plug-in Remote Code Execution (CTX134681)      1      High
Common Platform Enumeration (CPE)      4      None
Computer Manufacturer Information (WMI)      1      None
DCE Services Enumeration      4      None
Thanks Phillip, can I also use Count(Distinct Host)?  The code you provided works great, however the data table contains multiple risks for the same host.  I just want to count the single risk by host.
ASKER CERTIFIED SOLUTION
Avatar of Phillip Burton
Phillip Burton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!
Hi Phillip,

One more follow up question regarding this query you crafted for me.  For some reason I can not use the WHERE IN clause for the column risk. I am specifically looking for the number of Critical, High, etc.  The results provide the number of all rows with a risk.  See query below. Thank you.

Select Distinct [Host], Risk,
    (select Count (Distinct [Name])
    From [tablename] as U
    Where T.[Host] = U.[Host]) as CountOfDistinctRisk
From [tablename] as T
WHERE Risk IN ('Critical','High')
Group by [Host], Name, Risk
ORDER by [CountofDistinctRisk] DESC
Shouldn't it be inside the brackets?

Select Distinct [Host], Risk,
    (select Count (Distinct [Name])
    From [tablename] as U
    Where T.[Host] = U.[Host] AND Risk IN ('Critical','High')) as CountOfDistinctRisk
From [tablename] as T
Group by [Host], Name, Risk
ORDER by [CountofDistinctRisk] DESC

Open in new window

Having the AND statement inside of the sub query provides different results that are not accurate.  I actually do no know what it is reporting/resulting, essentially the countofdistinctrisk column produces a number that is not made up the total risks aggregate or something else.  We need the count of total number of critical, high, per hostname.
Sounds like this would work, if all you want is a filtered list:

Select [Name], 
    (select Count([Risk]) 
    From [tablename] as U
    Where T.[Name] = U.[Name]) as CountOfDistinctName,
Risk
From [tablename] as T
WHERE Risk IN ('Critical','High')
Group by [Name], Risk

Open in new window


I wouldn't understand why it wouldn't work.