sqlserver 求出连续三个月以上用户金额为0的数据

年度 月份 识别号 店名称 金额
2016 1 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 2 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 3 22010419580805261801 大庆市萨尔图区许驰比萨店 0.000000
2016 3 22010419580805261801 哈尔滨市南岗区许驰比萨店 0.000000
2016 3 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 4 22010419580805261801 大庆市萨尔图区许驰比萨店 0.000000
2016 4 22010419580805261801 哈尔滨市南岗区许驰比萨店 0.000000
2016 4 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 5 22010419580805261801 哈尔滨市南岗区许驰比萨店 0.000000
2016 5 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 6 22010419580805261801 哈尔滨市南岗区许驰比萨店 0.000000
2016 6 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 7 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 9 12232700556141546K 大兴安岭地区建设工程交易中心 0.000000
2016 10 22010419580805261801 大庆市萨尔图区许驰比萨店 0.000000
2016 10 22010419580805261801 哈尔滨市南岗区许驰比萨店 0.000000
2016 10 22010419580805261801 绥化市北林区城市比萨绥化店 0.000000
2016 10 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 11 12232700556141546K 大兴安岭地区建设工程交易中心 0.000000
2016 12 12232700556141546K 大兴安岭地区建设工程交易中心 0.000000
2016 08 23012219590524082601 阿城区天宇建材商店 0.000000
2016 09 23012219590524082601 阿城区天宇建材商店 0.000000
2016 11 23012219590524082601 阿城区天宇建材商店 0.000000
2016 12 23012219590524082601 阿城区天宇建材商店 0.000000

用sql求出连续三个以上金额为0的数据 结果为
2016 3 22010419580805261801 哈尔滨市南岗区许驰比萨店 0.000000
2016 4 22010419580805261801 哈尔滨市南岗区许驰比萨店 0.000000
2016 5 22010419580805261801 哈尔滨市南岗区许驰比萨店 0.000000
2016 6 22010419580805261801 哈尔滨市南岗区许驰比萨店 0.000000
2016 1 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 2 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 3 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 4 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 5 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 6 23010219580302041301 哈尔滨中央大街金店 0.000000
2016 7 23010219580302041301 哈尔滨中央大街金店 0.000000 收起 收起
弄了一小天没写出来,请各位帮忙看一下!谢谢!

如果是知道哪三个月的话可以这样 不知道的话我也没想出来
可能有些店某个月没有消费记录 你可以拿用户表左外连接 这个表

(case when xiaofei is null then 0 end) 然后就可以合计算了
select * from (
select cus_no,count(1) as cishu,sum(cast(xiaofei as DECIMAL)) as bb from (
select * from csdn_001 where (nian='2016' and yue='1') or (nian='2016' and yue='2') or(nian='2016' and yue='3')
)o GROUP BY cus_no
)oo where bb=0

create table csdn_001(
nian VARCHAR(5000),
yue VARCHAR(5000),
cus_no VARCHAR(5000),
cus_name VARCHAR(5000),
xiaofei VARCHAR(5000)
)
insert into csdn_001 values
('2015','1','23010219580302041301','哈尔滨中央大街金店',0.000000),
('2015','2','23010219580302041301','哈尔滨中央大街金店',0.000000),
('2015','3','23010219580302041301','哈尔滨中央大街金店',0.000000),
('2015','1','22010419580805261801','大庆市萨尔图区许驰比萨店',1.000000),
('2015','2','22010419580805261801','大庆市萨尔图区许驰比萨店',0.000000),
('2015','3','22010419580805261801','大庆市萨尔图区许驰比萨店',0.000000)

连续的没啥思路 我也百度一下看看

with sb as(
select * from (
select datepart(year, t.录入日期) 年度,
datepart(month,t.录入日期) 月份,
t.识别号,
t.店名称,
sum(t.应纳税额) 金额
FROM temp_申报信息 t
group by datepart(year, t.录入日期),
datepart(month,t.录入日期),
t.识别号,
t.店名称
) a where a.金额 = cast(0 as numeric(18,2)) and a.年度=2016
)

SELECT
*
FROM
(
select sb.年度,sb.月份, sb.识别号,sb.店名称,sb.金额,
lag(sb.月份,1,sb.月份-1) over( partition by sb.识别号,sb.店名称 order by sb.年度, sb.月份) 序号
from sb
)B where B.月份-B.序号=1
and B.店名称 in
( select 店名称 from ( select sb.年度,sb.月份, sb.识别号,sb.店名称,sb.金额,
lag(sb.月份,1,sb.月份-1) over( partition by sb.识别号,sb.店名称 order by sb.年度, sb.月份) 序号
from sb ) aa where aa.月份-aa.序号=1 group by 店名称 having count(1)>=3 )
order by 店名称,月份

这个是我写的但是求出的数据还是多

试试看,同一用户数据下,按ID和月份GROUP BY,再用lag和lead求连续三个月