sql server - T-SQL count of unique records by each month -
i trying count our customer base grew each month.
eg. "josh","tim" , "dustin" has used service in january, january number of new unique customers 3.
in february "josh","tim" , "eve" use service. "josh" , "tim" has used service before, number of new unique customers 1.
and on....
i wanted use except statement, not getting right results.
select count(distinct name) newuniquecustomers, convert(varchar(7), regdate, 126) t group convert(varchar(7), regdate, 126) except --this should excludie customers included select count(distinct name)as newuniquecustomers, convert(varchar(7), regdate, 126) t convert(varchar(7), dateadd(month,-1,regdate) , 126) group convert(varchar(7), regdate, 126)
using sqlfiddle should it.
with sorteddata ( select * , row_number() over(partition name order regdate) rownum t ) select dateadd(month, datediff(month, 0, regdate), 0) , count(name) sorteddata sd sd.rownum = 1 group dateadd(month, datediff(month, 0, regdate), 0)
--edit--
given comment needing pull 2 tables union why not this?
with sorteddata ( select * , row_number() over(partition name order regdate) rownum ( select * productiontable union select * archive.dbo.t ) x )
Comments
Post a Comment