Avatar of javilmer
 asked on

SQL QUESTION (SQL SERVER) : nb of occurences in a list


I have a table of personns (personnID, name) and a tables of commands (CommandRef, PersonnID), several commands being associated with one personn thru the PersonnID column.

So to see my commands and personns, I do :

select name, commandRef from personns left join commands on personns.personnID = commands.PersonnID

So I get something like :

Smith Ref-1
Smith Ref-2
Smith Ref-3
Taylor Ref-4
Johns Ref-5

I need to have the information that a name as several commands, so I need the listing to have a third column like :

Smith Ref-1 3
Smith Ref-2 3
Smith Ref-3 3
Taylor Ref-4 1
Johns Ref-5 1

Is there a "direct" way (without using a UDF function that counts because of performance problem) to get this third column in SQL ?

Any help greatly appreciated
Microsoft SQL Server

Avatar of undefined
Last Comment

8/22/2022 - Mon
Farzad Akbarnejad

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Thank you, this is perfect :)
Your help has saved me hundreds of hours of internet surfing.