分组排序查第一第二的差值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | select product_id, ABS (firstvalue-secondvalue)/secondvalue tmp from ( SELECT product_id, max ( case fund_subject when 'first' then fund_net_value end ) 'firstvalue' , max ( case fund_subject when 'second' then fund_net_value end ) 'secondvalue' FROM ( SELECT * FROM ( SELECT net1.product_id , 'first' as fund_subject , net1.fund_net_value_date as net_value_date ,net1.fund_net_value FROM pef_fund_net_value net1 where net1.fund_net_value_date = ( SELECT max (fund_net_value_date)_date from pef_fund_net_value WHERE product_id = net1.product_id ) ) am union SELECT * FROM ( SELECT a.product_id, 'second' as fund_subject , MAX (fund_net_value_date) as net_value_date , fund_net_value FROM ( SELECT net.* FROM pef_fund_net_value net LEFT JOIN ( SELECT product_id , max (fund_net_value_date) as maxdate FROM pef_fund_net_value GROUP BY product_id )net2 ON net.fund_net_value_date = net2.maxdate and net2.product_id = net.product_id where net2.maxdate is null ) a GROUP BY a.product_id ) pm ORDER by product_id ,net_value_date desc ) al group by product_id ) a4 where ABS (a4.firstvalue-a4.secondvalue)/a4.secondvalue > 0.05 -- 000001 first second net_value_date |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步