Solved

Count number of unique records for each user

Posted on 2014-11-07
3
142 Views
Last Modified: 2014-11-07
I have a table (tblMessage) that contains a lot of messages:

id
Text
Sender
Reciever
SiteId

I need to make a SQL that list out how many unique persons (Sender) that has contacted each user (Reciever), where siteId = [number], ordered by number of unique contacts.

Example: If a person (reciever) has got 2 messages from Jesscia, 2 messages from Lisa and 4 Messages from Mike, this should be counted as 3 (3 unique contacts).

The final result shoud be something like this:

Reciever   Contacts
------------------------------------
Jimmi        20
Sarah        12
Mike          11
Andrew     8
Lisa            5
Jessica       3
Jason         0

Thanks for all help :)
0
Comment
Question by:webressurs
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40427944
Select Reciever, Count(Distinct Sender) as Contacts
From tblMessage
Group by Receiver
Order by Count(Distinct Sender) DESC
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40427973
:) instead of the the last line above you can use

ORDER BY Contacts DESC, Reciever

(i.e. in the ORDER By clause you can reference column aliases)

no points please; it's just that Phillip didn't leave much for anyone else to do
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40428030
Quite right - the problems of answering quickly!

I've also noticed that in my Group By, the word "Receiver" was correctly spelt, whereas your field name is "Reciever".
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now