【问题标题】:Insert into select with a hard coded value with Ruby Sequel使用 Ruby Sequel 插入带有硬编码值的选择
【发布时间】:2018-11-19 14:22:05
【问题描述】:

我每天都在使用 Ruby Sequel 将数据移动到报表中。我正在将三个表中的数据合并到一个表中。我在 Redshift 中这样做,所以我必须使用 disable_insert_returning。两个表中的列名相互匹配,但不匹配最终表,这意味着我使用的是graphset_graph_aliases

reports = db[:reports]
report_columns = [:user_id, :purchase_date, :sku]

spoons_select_graph = {
  user_id: :users,
  purchase_date: :spoon_receipts,
  product_id: :spoon_receipts
}
spoons = db[:spoon_receipts]
spoons_select = spoons.graph(:users, user_id: :user_id).set_graph_aliases(spoons_select_graph)

forks_select_graph = {
  user_id: :users,
  purchase_date: :fork_receipts,
  product_id: :fork_receipts
}
forks = db[:fork_receipts]
forks_select = forks.graph(:users, user_id: :user_id).set_graph_aliases(forks_select_graph)

reports.disable_insert_returning.insert(report_columns, spoons_select)
reports.where(channel: nil).update(channel: 'spoons')
reports.disable_insert_returning.insert(report_columns, forks_select)
reports.where(channel: nil).update(channel: 'forks')

更新需要很长时间。我想做的是将频道添加到insert select,这样我就不必回去更新了。

【问题讨论】:

    标签: ruby amazon-redshift sequel


    【解决方案1】:

    您没有提供可执行设置,所以我没有对此进行测试,但我认为它会起作用。基本思想是在您的选择中添加一个恒定的结果列。

    reports = db[:reports]
    report_columns = [:user_id, :purchase_date, :sku, :channel]
    
    spoons_select_graph = {
      user_id: :users,
      purchase_date: :spoon_receipts,
      product_id: :spoon_receipts,
      channel: [:spoon_receipts, :channel, 'spoons']
    }
    spoons = db[:spoon_receipts]
    spoons_select = spoons.graph(:users, user_id: :user_id).set_graph_aliases(spoons_select_graph)
    
    reports.disable_insert_returning.insert(report_columns, spoons_select)
    

    有关更多信息,请参阅set_graph_aliases 的文档。

    【讨论】:

    • 谢谢!我在文档中错过了这个!
    • @Sixty4Bit 感谢您的赏金,我很高兴能提供帮助。还请将答案标记为已接受(单击大复选标记),以便当问题出现在以后的搜索中时,人们会看到它具有已接受的答案。
    • 您会认为授予赏金也会接受答案。奇怪的。感谢您的提醒。
    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 2017-08-27
    相关资源
    最近更新 更多