Avatar of adznon
adznon
 asked on

MSSQL Count

Hello,

I am looking for the SQL code to count distinct the number of rows that AuditModuleCode = Exp
 
Data Example

Store_Number AuditModuleCode
1                         Exp
1                         Sup
2                         Exp
2                         Exp
2                         Sup
3                         Exp
3                         Sup

Example output wanted

Store_Number AuditModuleCode
1                         1
2                         2
3                         1
Microsoft SQL Server

Avatar of undefined
Last Comment
Pawan Kumar

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
adznon

ASKER
Great, Thanks for the quick response worked perfectly
Pawan Kumar

Another option could be -

/*------------------------

SELECT DISTINCT Store_Number, COUNT(*) OVER(PARTITION BY Store_Number) AuditModuleCode FROM tst
WHERE AuditModuleCode = 'Exp'
------------------------*/
Store_Number AuditModuleCode
------------ ---------------
1            1
2            2
3            1

(3 row(s) affected)

Open in new window

Pawan Kumar

Welcome. Happy to help :)
Your help has saved me hundreds of hours of internet surfing.
fblack61
adznon

ASKER
Quick question how would you then add to only display if count is less than 8
Pawan Kumar

We need to add Having Clause.

SELECT Store_Number, COUNT(*) AuditModuleCode FROM tst
WHERE AuditModuleCode = 'Exp'
GROUP BY Store_Number
HAVING COUNT(*) < 8
adznon

ASKER
Perfect, cheers have a great day
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

Cheers.