数据库两列数据,第三列数据最大最小相差50的

本文深入解析了一段复杂的SQL查询代码,详细介绍了如何从多个表中筛选特定条件的数据,并通过分组、聚合函数以及子查询来统计和分析数据。特别关注了如何在SQL中处理日期时间字段、字符串操作以及条件过滤,为理解和优化大型数据库查询提供了实用指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求  根据同第一列 第二列相同  第三列数据>50 的统计结果

 

 

with p1 as (
select z.fldcztime,to_char(z.fldcztime,'yyyymmdd') as rq, t.accountname, z.snumber, z.lxcode, z.fldckuser,
 SUBSTR(t.ywbs,2) YWBS
  from wz_zdwzzbinfo z, t_zdddress t
 where z.lxcode = 'yssb'
   and z.fldstate = '待安装'
   and z.snumber = t.snumber
   AND t.ywbs LIKE 'B%' and t.ywbs not LIKE 'B0%' and t.ywbs not LIKE 'BO%' and t.ywbs not LIKE 'BT%' 
   and instr(t.ywbs,chr(10))=0 and instr(t.ywbs,chr(13))=0 and instr(t.ywbs,' ')=0 and instr(t.ywbs,' ')=0
   and instr(t.ywbs,'/')=0  and instr(t.ywbs,'~')=0 and t.ywbs is not null
   and instr(t.ywbs,'割')=0 and instr(t.ywbs,'故')=0 and instr(t.ywbs,'、')=0 and z.fldcztime is not null
 group by t.ywbs,
          z.fldcztime,
          z.snumber,
          z.lxcode,
          z.fldckuser,
          t.accountname
          ),
 P2 AS (         
 select p1.RQ,P1.ACCOUNTNAME ,count(p1.snumber) as hs from p1 group by p1.RQ,P1.ACCOUNTNAME  ),
 p3 as (
 select p2.rq,p2.accountname,p2.hs,
 (select max(p1.YWBS) from p1 where p1.rq=p2.rq and p1.accountname=p2.accountname) as maxbh,
 (select min(p1.YWBS) from p1 where p1.rq=p2.rq and p1.accountname=p2.accountname) as minbh
  from p2
 ) ,
 p4 as (
 select p3.RQ,P3.ACCOUNTNAME from p3 where (to_number(p3.maxbh)-to_number(p3.minbh))>50)
 select * from p1 where exists (select 1 from p4 where p4.rq=p1.rq and p4.ACCOUNTNAME=p1.ACCOUNTNAME)
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值