Hi,
I am using Eric DDL with some more data (since we need to check the distinct work OK)
-- DDL+DML
CREATE TABLE [A] (
[Year] INTEGER NULL,
[Month] INTEGER NULL,
[User] VARCHAR(255) NULL);
GO
truncate table A
INSERT INTO A([Year],[Month],[User])
VALUES
(2014,8,'Giacomo'),
(2014,9,'Leila'),
(2014,10,'Melodie'),
(2014,11,'Cora'),
(2014,12,'Amanda'),
(2015,1,'Stone'),
(2015,2,'Kaitlin'),
(2015,3,'Gloria'),
(2015,4,'Petra'),
(2015,5,'Kirsten'),
(2015,6,'Cally'),
(2015,7,'Alexander'),
(2015,8,'Ciaran'),
(2014,2,'Amanda'),
(2014,10,'Melodie'),
(2015,7,'Petra'),
(2015,7,'Ciaran');
select * from A
please try this:
declare @CurrentYear int = datepart(YEAR,GETDATE())
declare @CurrentMonth int = datepart(MONTH,GETDATE())
-- select @CurrentMonth,@CurrentYear
select
'number of distinct users',
[3 Months] = (
select count(distinct [user])from A
where
([Year] = @CurrentYear and [Month] > @CurrentMonth-3) -- data from this year
or
-- if @CurrentMonth < 3 then we need data from previous year as well:
-- If the "@CurrentMonth > 3" then this part "12 + (@CurrentMonth - 3)" is more then 12 and will not return any row
-- but If the "@CurrentMonth < 3" then this part "12 + (@CurrentMonth - 3)" is less then 12 and will return the missing data from previous year
([Year] = @CurrentYear - 1 and [Month] > 12 + (@CurrentMonth - 3)) -- data from previous year
),
-- same with 9 and 12
[6 Months] = (
select count(distinct [user]) from A
where
([Year] = @CurrentYear and [Month] > @CurrentMonth-6) -- data from this year
or
([Year] = @CurrentYear - 1 and [Month] > 12 + (@CurrentMonth - 6)) -- data from previous year
),
[9 Months] = (
select count(distinct [user]) from A
where
([Year] = @CurrentYear and [Month] > @CurrentMonth-9) -- data from this year
or
([Year] = @CurrentYear - 1 and [Month] > 12 + (@CurrentMonth - 9)) -- data from previous year
),
[12 Months] = (
select count(distinct [user]) from A
where
([Year] = @CurrentYear and [Month] > @CurrentMonth-12) -- data from this year
or
([Year] = @CurrentYear - 1 and [Month] > 12 + (@CurrentMonth - 12)) -- data from previous year
)
* My first solution do not using any calculation on the original columns [Year],[Month] so if you have (and
you probably should have) index on these column the query will probably use the index (I am not saying it will use the index if there are)
** we have no information regarding your real DDL, therefore, I can not say which query will be best or if indexes are relevant in your case, or if this query will use indexes in your case :-(
Another solution can be using CONVERT, which In some cases you can just use this as well, but check which is best in your specific case!
declare @FirstDayOfTheMonth DATE = DATEADD(DAY,1-(DATEPART(DAY,GETDATE())),GETDATE())
SELECT
'number of distinct users',
[3 Months] = (
select COUNT(DISTINCT [User])
from A
where DATEFROMPARTS([Year],[Month],01) > DATEADD(MONTH,-3,@FirstDayOfTheMonth)
),
[6 Months] = (
select COUNT(DISTINCT [User])
from A
where DATEFROMPARTS([Year],[Month],01) > DATEADD(MONTH,-6,@FirstDayOfTheMonth)
),
[9 Months] = (
select COUNT(DISTINCT [User])
from A
where DATEFROMPARTS([Year],[Month],01) > DATEADD(MONTH,-9,@FirstDayOfTheMonth)
),
[12 Months] = (
select COUNT(DISTINCT [User])
from A
where DATEFROMPARTS([Year],[Month],01) > DATEADD(MONTH,-12,@FirstDayOfTheMonth)
)
GO