We help IT Professionals succeed at work.
Get Started

Trying to get count on distinct multiple columns in SQL

sdeluc
sdeluc asked
on
635 Views
Last Modified: 2012-05-09
I have three columns used in a table I'm trying to get a distinct count on. userID, messageFrom, messageDate.

messaging is the table name
messageDate is smalldatetime in the format of MM/DD/YY 12:00:00 AM (always 12am)
userID is int, to whom the message is going to
messageFrom is nvarchar(128), who's sending the message

I'm trying to find the count of how many unique messageFrom's are sent to a particular userID on a daily basis from the beginning of the year. In this code, userID 1008. What's desired is if 2 identical messageFrom's are sent in the same day, it only counts as one. If 2 identical messageFrom's are sent in two days, it counts as two.

I'm unsure if the code below is correct as it's showing data, just much less than I expected.

Thanks....


SELECT COUNT(*) FROM 
(SELECT DISTINCT messageFrom,messageDate 
FROM messaging 
WHERE userID='1008' 
AND messageDate >= DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) AS t

Open in new window

Comment
Watch Question
SQL Server DBA & Architect, EE Solution Guide
CERTIFIED EXPERT
Awarded 2009
Distinguished Expert 2019
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE