B*****g 发帖数: 34098 | 1 原理一样,就是要达到排序后分组的目的
借用原题数据,第一步的目标加上ind1,指示此行是否是超过30天的行
User StartDate EndDate ind1
1 12/2/2011 1/16/2012 0
1 3/4/2012 3/24/2012 1
1 4/5/2012 4/26/2012 0
1 5/14/2012 6/7/2012 0
2 3/5/2012 7/30/2012 0
2 8/4/2012 9/15/2012 0
3 6/5/2012 8/20/2012 0
假设结果是t1,t1是这样得到的,请自行优化
select t11.*,
t11.startdate - (select MAX(t12.enddate) from t t21 where .. < ..) ind11
from t t11
ind11是相差的天数,自行转化成ind1 0(<=30),1(>30)
借用原题数据,第二步的目标加上ind2,指示分组
User StartDate EndDate ind1 ind2
1 12/2/2011 1/16/2012 0 0
1 3/4/2012 3/24/2012 1 1
1 4/5/2012 4/26/2012 0 1
1 5/14/2012 6/7/2012 0 1
2 3/5/2012 7/30/2012 0 0
2 8/4/2012 9/15/2012 0 0
3 6/5/2012 8/20/2012 0 1
假设结果是t2,t2是这样得到的,请自行优化
select t21.*,
(select sum(ind1) from t1 t22 where ...<=....) ind2
from t1 t21
分组成功,min,max |
|