Вы можете нормализовать даты в группы по 2, преобразовав их в числовое целочисленное значение и уменьшив до четных чисел.Простой способ сделать это - val / 2 * 2
, потому что первый / 2
будет обрезан до десятичных разрядов (если тип val
является целым числом!), А * 2
вернет его к исходномузначение, кроме нормированного на четное число.Вот пример, который нормализует и группирует результаты, используя источник данных CTE:
;with Data as (
select '1/1/2011' as [date], 1 as x union
select '1/1/2011' as [date], 2 as x union
select '1/1/2011' as [date], 3 as x union
select '1/1/2011' as [date], 4 as x union
select '1/1/2011' as [date], 5 as x union
select '1/1/2011' as [date], 6 as x union
select '1/1/2011' as [date], 7 as x union
select '1/1/2011' as [date], 8 as x union
select '1/1/2011' as [date], 9 as x union
select '1/1/2011' as [date], 10 as x union
select '1/2/2011' as [date], 11 as x union
select '1/2/2011' as [date], 12 as x union
select '1/2/2011' as [date], 13 as x union
select '1/2/2011' as [date], 14 as x union
select '1/2/2011' as [date], 15 as x union
select '1/3/2011' as [date], 16 as x union
select '1/3/2011' as [date], 17 as x union
select '1/3/2011' as [date], 18 as x union
select '1/3/2011' as [date], 19 as x union
select '1/3/2011' as [date], 20 as x union
select '1/3/2011' as [date], 21 as x union
select '1/3/2011' as [date], 22 as x union
select '1/3/2011' as [date], 23 as x union
select '1/4/2011' as [date], 24 as x union
select '1/4/2011' as [date], 25 as x union
select '1/4/2011' as [date], 26 as x
)
Select
cast(cast(cast(Date as datetime) as integer) / 2 * 2 as datetime) as date,
count(x)
from data
group by cast(cast(Date as datetime) as integer) / 2 * 2
Вывод:
date (No column name)
2011-01-01 00:00:00.000 15
2011-01-03 00:00:00.000 11