我有一个看起来像这样的表:
Name Post Like Share Comment Date
--------------------------------------------
Sita test data 1 5 2 4 28/4/2015
Munni test data 2 5 2 5 27/4/2015
Shila test data 3 1 3 1 22/4/2015
Ram Test data 4 5 0 5 1/4/2015
Sam Test data 5 4 0 2 2/4/2015
Jadu Test data 6 1 5 2 30/3/2015
Madhu Test data 7 5 0 4 10/4/2015
现在我想要我的结果集如下:
Type Name Post Like Share Comment Date
-------------------------------------------------------------------------
Today Sita test data 1 5 2 4 28/4/2015
Last 7 Days Sita test data 1 5 2 4 28/4/2015
Last 7 Days Munni test data 2 5 2 5 27/4/2015
Last 7 Days Shila test data 3 1 3 1 22/4/2015
Last 30 Days Sita test data 1 5 2 4 28/4/2015
Last 30 Days Munni test data 2 5 2 5 27/4/2015
Last 30 Days Shila test data 3 1 3 1 22/4/2015
Last 30 Days Ram Test data 4 5 0 5 1/4/2015
Last 30 Days Sam Test data 5 4 0 2 2/4/2015
Last 30 Days Jadu Test data 6 1 5 2 30/3/2015
Last 30 Days Madhu Test data 7 5 0 4 10/4/2015
今天必须只有今天的帖子。过去7天必须有今天的帖子+过去7天的帖子。最近30天列必须包含过去30天的所有帖子。
答案 0 :(得分:0)
一些具有不同案例陈述的工会可以获得日期范围。
答案 1 :(得分:0)
使用union all
和dateadd
:
select 'Today' as Type, Name, Post, [Like], Share, Comment, [Date]
from yourtable
where [Date] = getdate()
union all
select 'Last 7 Days' as Type, Name, Post, [Like], Share, Comment, [Date]
from yourtable
where [Date] >= DateAdd(day,-7,getdate())
union all
select 'Last 30 Days' as Type, Name, Post, [Like], Share, Comment, [Date]
from yourtable
where [Date] >= DateAdd(day,-30,getdate())
BTW,列名的可怕选择(不要使用保留字)。