字段A, 字段B, 字段C
abc 200 20160201
abc 300 20160202
abc 400 20160202
bcd 300 20160201
bcd 200 20160202
bcd 300 20160203
找出字段B间隔一天增量超过100的记录,怎么做到,望大神解答
1 个解决方案
#1
你的隔一天是指第二天还是第三天,我的例子是第二天的,看着来修改
WITH CTE (ColA,ColB,ColC) AS
(
SELECT 'abc',CONVERT(INT,'200'),CONVERT(DATE,'20160201')
UNION ALL
select 'abc','300','20160202'
UNION ALL
select 'abc','400','20160202'
UNION ALL
select 'bcd','300','20160201'
UNION ALL
select 'bcd','200','20160202'
UNION ALL
select 'bcd','300','20160203'
),CTE2 AS
(
SELECT ColA,ColC,SUM(ColB) AS ColB
FROM CTE
GROUP BY ColA,ColC
)
SELECT a.*
FROM CTE2 a
INNER JOIN CTE2 b ON a.ColA = b.ColA AND b.ColB > a.ColB + 100 AND b.ColC = DATEADD(dd,1,a.ColC)
#1
你的隔一天是指第二天还是第三天,我的例子是第二天的,看着来修改
WITH CTE (ColA,ColB,ColC) AS
(
SELECT 'abc',CONVERT(INT,'200'),CONVERT(DATE,'20160201')
UNION ALL
select 'abc','300','20160202'
UNION ALL
select 'abc','400','20160202'
UNION ALL
select 'bcd','300','20160201'
UNION ALL
select 'bcd','200','20160202'
UNION ALL
select 'bcd','300','20160203'
),CTE2 AS
(
SELECT ColA,ColC,SUM(ColB) AS ColB
FROM CTE
GROUP BY ColA,ColC
)
SELECT a.*
FROM CTE2 a
INNER JOIN CTE2 b ON a.ColA = b.ColA AND b.ColB > a.ColB + 100 AND b.ColC = DATEADD(dd,1,a.ColC)