I have to get the users, which appear at least two weeks before and after the given date. So lets say I have data:
userName date week
user1 27 10 2011 44
user1 27 10 2011 44
user1 27 10 2011 44
user2 21 04 2011 17
user2 29 04 2011 17
user2 02 05 2011 19
user2 03 05 2011 19
user2 16 05 2011 21
user2 23 05 2011 22
user3 06 01 2011 24
user3 14 05 2011 25
user3 20 05 2011 26
user3 27 05 2011 27
and I need to get the results first grouped by user and week, then I need to count how many weeks the user appears before (lets say week 20) and after, and then select only ones who appears at least 2 weeks before and after, so in my case I would get the result
user2
Unfortunately I cannot create viewTable because of the database restrictions. this query is giving me only the first part of the results, data grouped by user and week, but I have no idea how to count grouped data:
SELECT username,
min(a.actionDate) as date,
datepart(wk,a.actionDate) as week
FROM Table1 a
GROUP BY username ,
datepart(wk,amd.actionDate)
thanks for any help.
To return users which have date records at least two weeks before and two weeks after a specified date, try:
select username
from Table1
group by username
having datediff(wk, min(actiondate), @date) >= 2 and
datediff(wk, @date, max(actiondate)) >= 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.