RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1592559
Accepted
Alexandr
Alexandr
Asked:2024-09-01 21:13:00 +0000 UTC2024-09-01 21:13:00 +0000 UTC 2024-09-01 21:13:00 +0000 UTC

将之前期间最后非零行的值的总和附加到表中

  • 772

有表1格式:

提供者 日期
001 2000年3月1日
001 2000年4月1日

(供应商 > 1) 1个月内,1个供应商只能有一个日期,月份可以跳过

表2格式:

提供者 合同 日期 和
001 111111 2000年1月1日 100
001 111111 2000年3月1日 150
001 222222 2000年1月1日 200
001 222222 2000年4月1日 250

(> 1 个供应商,> 每个供应商 1 个合同)

如何将第二个与第一个连接起来,以便第一个具有供应商和合同金额的结果列。如果缺少日期,我们将采用最后一个已知日期。

提供者 日期 结果
001 2000年3月1日 150+200
001 2000年4月1日 150+250

根据合同 111111,为期 03 个月 150,对于 222222 最后已知金额 200 根据合同 111111,对于 04 个月,最后已知金额 150,对于 222222 250

postgresql
  • 1 1 个回答
  • 24 Views

1 个回答

  • Voted
  1. Best Answer
    ValNik
    2024-09-02T19:04:23Z2024-09-02T19:04:23Z

    例子

    提供者 日期
    001 2000-03-01
    001 2000-04-01
    提供者 合同 日期 总结
    001 111111 2000-01-01 100
    001 111111 2000-03-01 150
    001 222222 2000-01-01 200
    001 222222 2000-04-01 250

    我们将表 2 中给定供应商的所有行附加到表 1 的每一行,并在每个合同中按降序对日期进行编号。

    select *
      ,row_number()over(partition by t1.provider,t1.date,t2.contract
                    order by t2.date desc)rn
    from table1 t1
    left join table2 t2 on t2.provider=t1.provider
      and t2.date<=t1.date
    order by t1.provider,t1.date,t2.date
    
    提供者 日期 提供者 合同 日期 总结 rn
    001 2000-03-01 001 111111 2000-01-01 100 2
    001 2000-03-01 001 222222 2000-01-01 200 1
    001 2000-03-01 001 111111 2000-03-01 150 1
    001 2000-04-01 001 222222 2000-01-01 200 2
    001 2000-04-01 001 111111 2000-01-01 100 2
    001 2000-04-01 001 111111 2000-03-01 150 1
    001 2000-04-01 001 222222 2000-04-01 250 1

    最后总结

    select provider,date,sum(summa)summa
      ,string_agg(cast(summa as varchar),'+')s
    from(
      select t1.provider,t1.date,t2.summa,t2.date rdate
        ,row_number()over(partition by t1.provider,t1.date,t2.contract
                          order by t2.date desc)rn
      from table1 t1
      left join table2 t2 on t2.provider=t1.provider
             and t2.date<=t1.date
    )a
    where rn=1
    group by provider,date
    order by provider,date
    
    提供者 日期 总结 s
    001 2000-03-01 350 150+200
    001 2000-04-01 400 150+250

    小提琴

    • 1

相关问题

  • 表不可见

  • 如何修复 postgresql 堆栈深度限制超出错误?

  • 如何为 PostgreSQL 添加 ENUM

  • PostgreSQL:如何在冲突中执行插入操作?

  • 在哪里可以看到所有 postgre 宏?

  • SQL。将SELECT结果合并为一行,需要将得到的结果替换为可读的结果

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5