Writing a SQL Server Query to find Totals

Hi Experts,

I want to write a SQL Server query for the attached table  to find the below:
1- Total number of service related to Consumer # AC056 without duplication.
2- Total number of datasets related to Consumer # AC056 without duplication.
3- Total number of providers related to Consumer # AC056 without duplication.
4- Total number of datasets for each service ID without duplication.

Capture.PNG


Thanks a lot.
Harreni
HarreniAsked:
Who is Participating?
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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Please define when a record is duplicated. Which columns should be considered? Which columns are the primary key?
Use the above table to mark the duplicated records.
Docteur_ZCommented:
As I can understand your request, I'd say :
For points 1 to 3
select ConsumerID as Consummer,count(distinct ServiceID) as NbService, count(distinct DatasetID) as NbDataset,
count(distinct ProviderId) as NbProvider
from Table
group by ConsumerID;

Open in new window


For point n°4
select ServiceID, count(distinct DatasetID) as NbDataset
from Table
group by ServiceID;

Open in new window


Don't forget if you have null(s) they don't count.

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
sdstuberCommented:
select count(distinct serviceid) from yourtable where consumerid = 'AC056';

select count(distinct datasetid) from yourtable where consumerid = 'AC056';

select count(distinct providerid) from yourtable where consumerid = 'AC056';

select count(distinct datasetid) from yourtable where consumerid = 'AC056';
HarreniAuthor Commented:
Thanks a lot Vitor, Docteur_Z and sdstuber.
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.