如何用sql查询出连续三个月金额大于50的记录
Posted hondahsu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用sql查询出连续三个月金额大于50的记录相关的知识,希望对你有一定的参考价值。
with tmp as
(
select ‘A‘ AS USERNAME,to_date(‘2017.10‘,‘yyyy.mm‘) month,45 num from dual union all
select ‘A‘ AS USERNAME,to_date(‘2017.11‘,‘yyyy.mm‘) month,53 from dual union all
select ‘A‘ AS USERNAME,to_date(‘2017.12‘,‘yyyy.mm‘) month,59 from dual union all
select ‘B‘ AS USERNAME,to_date(‘2018.1‘,‘yyyy.mm‘) month,78 from dual union all
select ‘B‘ AS USERNAME,to_date(‘2018.2‘,‘yyyy.mm‘) month,69 from dual union all
select ‘B‘ AS USERNAME,to_date(‘2018.3‘,‘yyyy.mm‘) month,51 from dual union ALL
select ‘B‘ AS USERNAME,to_date(‘2018.5‘,‘yyyy.mm‘) month,49 from dual union ALL
select ‘B‘ AS USERNAME,to_date(‘2018.4‘,‘yyyy.mm‘) month,51 from dual union ALL
select ‘B‘ AS USERNAME,to_date(‘2018.6‘,‘yyyy.mm‘) month,49 from dual union ALL
select ‘A‘ AS USERNAME,to_date(‘2017.9‘,‘yyyy.mm‘) month,52 from dual union ALL
select ‘A‘ AS USERNAME,to_date(‘2017.7‘,‘yyyy.mm‘) month,49 from dual union ALL
select ‘A‘ AS USERNAME,to_date(‘2017.8‘,‘yyyy.mm‘) month,55 from dual
)
select USERNAME,month start_month,num,num2,num3 from
( select USERNAME,
month,
num,
lead(num, 1, null) over(PARTITION BY username order by month) num2,
lead(num, 2, null) over(PARTITION BY username order by month) num3
from tmp order by username,month)
--where num >=50 and num2 >=50 and num3 >=50
以上是关于如何用sql查询出连续三个月金额大于50的记录的主要内容,如果未能解决你的问题,请参考以下文章
如何用sql的日期函数,分别查出1月~12月每个月的销售金额?