SQL每隔15天在一段时间内获取记录

时间:2013-10-07 07:19:14

标签: sql sql-server sql-server-2008

我的数据库中有2万个数据,需要根据'uploaddatetime'获取记录 (每15天一次数据库中的列)。例如,

Date               Number of Records.
April 1st-15th            20
April 16th-30th           40
May 1st to 15th           1000
May 16th to 31st          4000

到目前为止,有任何想法使用Microsoft SQL-2008 R2获取数据

1 个答案:

答案 0 :(得分:1)

你可以做这样的事情

select 
case 
when date_col>='20130401' and date_col<'20130416' then 'April 1st-15th'
when date_col>='20130416' and date_col<'20130501' then 'April 16th-30th'
.
.
as date_range
end
count(*) as total
from table
group by 
when date_col>='20130401' and date_col<'20130416' then 'April 1st-15th'
when date_col>='20130416' and date_col<'20130501' then 'April 16th-30th'
.
.
end