troubleshooting Question

Trying to get count on distinct multiple columns in SQL

Avatar of sdeluc
sdelucFlag for United States of America asked on
Microsoft SQL Server 2005
6 Comments1 Solution636 ViewsLast Modified:
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.


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

Open in new window

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros