力扣 585.2016年的投资

数据准备

Create Table If Not Exists Insurance (pid int, tiv_2015 float, tiv_2016 float, lat float, lon float);
Truncate table Insurance;
insert into Insurance (pid, tiv_2015, tiv_2016, lat, lon) values ('1', '10', '5', '10', '10');
insert into Insurance (pid, tiv_2015, tiv_2016, lat, lon) values ('2', '20', '20', '20', '20');
insert into Insurance (pid, tiv_2015, tiv_2016, lat, lon) values ('3', '10', '30', '20', '20');
insert into Insurance (pid, tiv_2015, tiv_2016, lat, lon) values ('4', '10', '40', '40', '40');

输入

在这里插入图片描述

输出

with t1 as (
    select *,
           count(tiv_2015) over (partition by tiv_2015) rn1,
           count(1) over (partition by lat,lon) rn2
    from Insurance
),
-- 求tiv_2015中重复的id
t2 as (
    select pid
    from t1
    where rn1>1
),
-- 两表关联求出tiv_2015重复的所有数据
t3 as (
    select i.*
    from t2,insurance i
    where t2.pid=i.pid
),
-- 求精度和纬度不重复的数据
t4 as (
    select *
    from t1
    where rn2=1
)
-- 求2016年的总投资额
select sum(t4.tiv_2016) as TIV_2016
from t4,t3
where t3.pid=t4.pid

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值