【问题标题】:Bring the data from two tables into one table with where clause使用where子句将两个表中的数据合并到一个表中
【发布时间】:2017-01-15 13:40:37
【问题描述】:

我有两个文件已批量导入到两个日期重叠的表中。现在我想将两个表中的数据移动到一个新表中,但删除重叠的日期。这是我的代码:

-- Pull across data from P1-10 and exclude out of scope dates
SELECT      *
INTO        I_Inventory
FROM        I_Inventory_P10_Temp
WHERE       [date] >= '01/01/2016' and [date] <= '10/31/16'

-- Pull across data from P11-12 and exclude out of scope dates
SELECT      *
INTO        I_Inventory
FROM        I_Inventory_P12_Temp
WHERE       [date] >= '11/01/2016' and [date] <= '12/31/16'

但是这不起作用,因为它说在第一次选择后表已经存在。有人可以让我知道如何解决这个问题吗?我对 SQL 很陌生。

【问题讨论】:

  • 查看 UNION ALL。

标签: sql sql-server database


【解决方案1】:

使用UNION ALL合并结果,然后插入到表中

SELECT      *
INTO        I_Inventory
FROM        I_Inventory_P10_Temp
WHERE       [date] >= '01/01/2016' and [date] <= '10/31/16'
Union All
-- Pull across data from P11-12 and exclude out of scope dates
SELECT      *
FROM        I_Inventory_P12_Temp
WHERE       [date] >= '11/01/2016' and [date] <= '12/31/16'

【讨论】:

  • 两个不同的表,所以前两个查询不起作用。
  • @jarlh - 谢谢没看到 ;)
猜你喜欢
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 2018-06-23
  • 2018-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多