sql server count

I have a table full of schedules. Each schedule is owned by a client. I want to know how many schedules each client owns.

Please help. thanks!

select distinct count(scheduleId), clientname from schedule
inner join client on client.clientid = schedule.clientid
group by scheduleid, clientname

Open in new window

LVL 2
Starr DuskkASP.NET VB.NET DeveloperAsked:
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.

chaauCommented:
You do not need to group by scheduleid. Just group by clientname. In addition to the clientname I would add clientid to eliminate the possibility of merging the schedule counts for the clients with the same name:
select count(scheduleId), schedule.clientid, client.clientname from schedule
inner join client on client.clientid = schedule.clientid
group by schedule.clientid, client.clientname 

Open in new window

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
PortletPaulEE Topic AdvisorCommented:
one NEVER (never ever!) needs BOTH "select distinct" and "group by" in a single query

GROUP BY produces rows that are unique for the combination of columns you nominate in that clause

SELECT DISTINCT - which is performed AFTER the group by - then checks if all the rows are unique, but they already are, so "select distinct" is just a waste of effort when you are doing group by too.

Chaau's query is absolutely correct just note you do not need to include schedule.clientid in the group or the select clauses, like this:
select count(scheduleId), client.clientname from schedule
inner join client on client.clientid = schedule.clientid
group by client.clientname 
order by client.clientname 

Open in new window

no points please
Vikas GargAssociate Principal EngineerCommented:
Hi,

Hope this query will help you if you have duplicate scheduleid then yo have to use Distinct in the count

select  count(distinct scheduleId), clientname from schedule
inner join client on client.clientid = schedule.clientid
group by clientname

Open in new window

IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Olaf DoschkeSoftware DeveloperCommented:
With a little tricky use of MIN you can have the advantage to group by the unique clientid and still show the clientname in the result:

select  count(distinct scheduleId) as schedulecount, schedule.clientid, 
MIN(schedule.clientname) as clientname
from schedule inner join client on client.clientid = schedule.clientid
group by schedule.clientid

Open in new window

Group By needs every single standing field to be grouped or aggregated, since all clientnames, so not grouping on clientname would cause an error, but since any clientid has just one clientname, MIN(clientname) is the same as clientname per clientid and satisfies the group by rules.

If you don't need the name you don't even need a join and it all boild down to
select  count(distinct scheduleId) as schedulecount, clientid from schedule
group by clientid

Open in new window


Bye, Olaf.
ZberteocCommented:
You only need the schedule table because you have a clientid value associated with any schedule:
select 
    clientid,
    count(*) as schedule_count
from
    schedule
group by 
    clientid

Open in new window

If you need the names then you can join:
select 
    clientid,
    min(clientname) as clientname,
    count(*) as schedule_count
from
    schedule s
    inner join client c
	   on c.clientid=s.clientid
group by 
    s.clientid

Open in new window

Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
thanks all!
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.