Search This Blog

Wednesday, January 12, 2011

Solve:

select *, WEEKDATECOUNT = (SELECT COUNT(DISTINCT column2) FROM test e
WHERE e.column1=tt.column1 and e.column2 between tt.start_date and tt.end_Date) ,
MONTHDATECOUNT
= (SELECT COUNT(DISTINCT column2) FROM test e
WHERE e.column1=tt.column1 and e.column2 between tt.month_first and tt.month_end) FROM
(SELECT DISTINCT COLUMN1, COLUMN2 - (DATEPART(dw,COLUMN2)- 1) as start_date ,
COLUMN2
+ (7 - DATEPART(dw, COLUMN2)) as end_date,
DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,column2)+1,0)) as month_end ,
DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,column2),0)) as month_first from test k)
tt

No comments:

Post a Comment