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
adznonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please try this -

Explanation - You need to use GROUP BY Store_Number and then take the COUNT to get the number of AuditModuleCode values.

CREATE TABLE tst
(
	Store_Number INT 
	, AuditModuleCode VARCHAR(10)
)
GO

INSERT INTO tst VALUES
(1,'Exp'),
(1,'Sup'),
(2,'Exp'),
(2,'Exp'),
(2,'Sup'),
(3,'Exp'),
(3,'Sup')
GO

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

Open in new window


OUTPUT

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

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

(3 row(s) affected)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adznonAuthor Commented:
Great, Thanks for the quick response worked perfectly
0
Pawan KumarDatabase ExpertCommented:
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

0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Pawan KumarDatabase ExpertCommented:
Welcome. Happy to help :)
0
adznonAuthor Commented:
Quick question how would you then add to only display if count is less than 8
0
Pawan KumarDatabase ExpertCommented:
We need to add Having Clause.

SELECT Store_Number, COUNT(*) AuditModuleCode FROM tst
WHERE AuditModuleCode = 'Exp'
GROUP BY Store_Number
HAVING COUNT(*) < 8
0
adznonAuthor Commented:
Perfect, cheers have a great day
0
Pawan KumarDatabase ExpertCommented:
Cheers.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.