Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rchoi1203
Starting Member
2 Posts |
Posted - 2012-02-27 : 17:29:17
|
| Hi all,Sorry for this rather basic question, but I'm trying to return the number of distinct userids if they have logged in during two different date ranges to get something like a weekly or monthly 'return' number.I know the 'having' clause wouldn't work and I'm not sure what function I should be using.As an fyi, this is a session table I'm referencing and the relevant parameters are 'userid' and 'startdate'.Thanks for any help anyone can provide. |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-02-27 : 17:33:21
|
| You can use the EXISTS clause. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-02-27 : 17:52:01
|
| [code]SELECT COUNT(DISTINCT UserID)FROM TableWHERE StartDate BETWEEN @StartDate and @EndDate OR StartDate BETWEEN @StartDate1 and @EndDate1[/code]Or maybe[code]SELECT COUNT(DISTINCT A.UserID)FROM( SELECT UserID FROM Table WHERE StartDate BETWEEN @StartDate and @EndDate) AS AINNER JOIN( SELECT UserID FROM Table WHERE StartDate BETWEEN @StartDate1 and @EndDate1) AS BON A.UserID = B.UserID[/code] |
 |
|
|
rchoi1203
Starting Member
2 Posts |
Posted - 2012-02-27 : 19:29:56
|
| Thanks all!Lamprey,I used your the second query and it seemed to have worked like a charm. I didn't really know you could join the same table together with different data sets, that's just genius and something I couldn't think of.Thanks again! |
 |
|
|
|
|
|