【问题标题】:Update one record in daily table from monthly从每月更新每日表中的一条记录
【发布时间】:2017-03-15 18:22:23
【问题描述】:

我有两个文件,一个是每日文件,第二个是每月文件。现在每天的文件都会每天加载到一个表中。

我们在下个月的第 1 周获得月度文件(例如,在月度文件中,我们有 1 月 1 日至 1 月 31 日的数据,我们将在 2 月 1 日收到此文件)。 加载每日文件数据后,我们将更新每月文件,现在我应该添加一列作为状态。

“当订单进入每日或每月订阅时,将状态设为已预订。当订单不是按月供稿但出现在每日供稿中时,您应将状态更新为 CANCELLED”

谁为此编写 SQL??任何建议。

假设我们有事实表

+----------+--------------------+-----------+ 
|order_id | booking_date | status | 
| 100 | 2017-02-10 | booked |
| 101 | 2017-02-12 | booked | 
+----------+--------------------+-----------+ 

在每月文件中

+----------+--------------------+ 
|order_id | booking_date |
|100 | 2017-02-10 | 
+-----------+-------------------+ 

我想要这样的结果

+----------+--------------------+-------------+ 
|order_id | booking_date | status |
| 100 | 2017-02-10 | booked | 
| 101 | 2017-02-12 | cancelled|

【问题讨论】:

  • 一些样本数据和期望的结果会很棒
  • 假设在事实表中我们有 |order_id |预订日期 |状态 | | 100 | 2017-02-10 |预订 | | 101 | 2017-02-12 |预订 |在每月文件中 |order_id |预订日期 | |100 | 2017-02-10 |我想要这样的结果 |order_id |预订日期 |状态 | | 100 | 2017-02-10 |预订 | | 101 | 2017-02-12 |取消 |

标签: mysql sql vertica


【解决方案1】:

非常简单。将您的月度文件加载到数据库中,然后使用它:

update MyTable
set status = 'Cancelled'
where Order_ID not in (select Order_ID from Monthly_File)

【讨论】:

  • 在 Monthlyfile 中,我们将只有 3 月的数据正确,如果我使用此查询,那么它将更新所有剩余月份数据为已取消。例如,当我获得 3 月份的月度文件时,它应该只需要 MYtable 中的 3 月份数据
  • 终于明白了,非常感谢!!!! @JohnHC update MYtable set status='cancelled' where (order_id,order_date) not in (select order_id,order_date from Monthly file)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多