17,140
社区成员




with tmp as
(select '88888888' as phone, '201006' as month, 54 as expenses from dual union all
select '88888888' as phone, '201007' as month, 80 as expenses from dual union all
select '88888888' as phone, '201008' as month, 60 as expenses from dual union all
select '88888888' as phone, '201009' as month, 0 as expenses from dual union all
select '88888888' as phone, '201010' as month, 0 as expenses from dual union all
select '77777777' as phone, '201006' as month, 54 as expenses from dual union all
select '77777777' as phone, '201007' as month, 80 as expenses from dual union all
select '77777777' as phone, '201008' as month, 23 as expenses from dual union all
select '77777777' as phone, '201009' as month, 0 as expenses from dual union all
select '77777777' as phone, '201010' as month, 0 as expenses from dual )
select phone
from tmp t
where month in ('201006', '201007', '201008', '201009', '201010')
and ((month in ('201009', '201010') and expenses = 0) or
(month in ('201006', '201007', '201008') and expenses between 51 and 100))
group by phone
having count(1) = 5
select phone from
(select phone,count(1) cnt from
(select * from jifei where month in(201006,201007,201008) and expenses > 0
Intersect
select * from jifei where month in(201009,201010) and expenses = 0)
where expenses between 51 and 100
group by phone)
where cnt = 3
;
用Intersect取并集的方法更直观一点
select phone from
(select phone,count(1) cnt from
(select * from jifei where month in(201006,201007,201008) and expenses > 0
minus
select * from jifei where month in(201009,201010) and expenses > 0)
where expenses between 51 and 100
group by phone)
where cnt = 3
;
再加一层嵌套查询了
select * from
(select * from jifei where month in(201006,201007,201008) and expenses > 0
minus
select * from jifei where month in(201009,201010) and expenses > 0)
where expenses between 51 and 100
;
select * from
(select * from jifei where month in(201006,201007,201008)
minus
select * from jifei where month in(201009,201010))
where expenses between 51 and 100
;