【发布时间】:2021-07-29 11:24:27
【问题描述】:
我有一个问题,我需要根据last_update 数据或last_transaction_product 将表中的选定行插入到另一个表中。 last_update是从表中更新的数据,last_transaction_product是我店的最新交易,然后我的行有重复数据但是这里有不同的last_update列,这个重复数据一定不能插入到我的目的地表在这里,因为数据已经插入到目标表中。
例如,我这里有 3 张桌子。
- 源表 AS
Table_Source - 目标表 AS
Table_Destination - 日志表 AS
Table_Log
我的问题是,我的 Source Table 和 Destination Table 没有任何 PK 可用于 Row 表的标识。
- 这是我的
Table_Source
| product_name | shop_name | last_transaction_product | last_update |
|---|---|---|---|
| Clock | Benefical | 2020-04-15 | 2020-06-01 |
| Monitor | Hinezo Computer | 2020-01-17 | 2020-06-01 |
| Notebook | LA Shop | 2020-03-25 | 2020-06-01 |
- 我有一个查询,它根据
> @last_transaction_product或> @last_update将其插入Table_Destination,这是我从Table_Source插入后的Table_Destination,我在2020-06-02 插入它
| product_name | shop_name | last_transaction_product | last_update |
|---|---|---|---|
| Clock | Benefical | 2020-04-15 | 2020-06-01 |
| Monitor | Hinezo Computer | 2020-01-17 | 2020-06-01 |
| Notebook | LA Shop | 2020-03-25 | 2020-06-01 |
- 插入我的
Table_Destination后,会将最新活动表中的记录保存到Table_Log,所以这里插入后是我的Table_Log。
| table_name | last_transaction_product | last_update | last_run |
|---|---|---|---|
| Table_Source | 2020-04-15 | 2020-06-01 | 2020-06-02 |
- 下一次,在
Table_Source处插入了新数据
| product_name | shop_name | last_transaction_product | last_update |
|---|---|---|---|
| Clock | Benefical | 2020-04-15 | 2020-06-01 |
| Monitor | Hinezo Computer | 2020-01-17 | 2020-06-01 |
| Notebook | LA Shop | 2020-03-25 | 2020-06-01 |
| Poster | Maniac Shop | 2020-04-27 | 2020-06-03 |
| Clock | Benefical | 2020-04-15 | 2020-06-03 |
- 从
Table_Source的最新表中可以看到,有一个重复的数据('Clock', 'Benefical', '2020-04-15', '2020-06-03')具有不同的last_update。就我而言,我想排除这个重复的数据以插入我的Table_Destination,所以它只会插入('Poster', 'Maniac Shop', '2020-04-27', '2020-06-03'),当然我使用最新的last_transaction_product和last_update来自Table_Log作为我的参数插入Table_Destination。所以我的Table_Destination和我的Table_Log会是这样的。
| product_name | shop_name | last_transaction_product | last_update |
|---|---|---|---|
| Clock | Benefical | 2020-04-15 | 2020-06-01 |
| Monitor | Hinezo Computer | 2020-01-17 | 2020-06-01 |
| Notebook | LA Shop | 2020-03-25 | 2020-06-01 |
| Poster | Maniac Shop | 2020-04-27 | 2020-06-03 |
| table_name | last_transaction_product | last_update | last_run |
|---|---|---|---|
| Table_Source | 2020-04-15 | 2020-06-01 | 2020-06-02 |
| Table_Source | 2020-04-27 | 2020-06-03 | 2020-06-04 |
现在这是我的 T-SQL
BEGIN
-- INSERT DESTINATION DATA --
SET Table_Destination ON;
INSERT INTO
Table_Destination
SELECT
*
FROM
Table_Source
WHERE
last_transaction_product > (SELECT MAX(last_transaction_product) FROM Table_Log WHERE table_name = 'Table_Source')
OR
last_update > (SELECT MAX(last_update) FROM Table_Log WHERE table_name = 'Table_Source')
SET Table_Destination OFF;
-- INSERT LOG UPDATE --
INSERT INTO Table_Log(table_name, last_transaction_product, last_update, last_run)
VALUES('Table_Source', (SELECT MAX(last_transaction_product) FROM Table_Source WHERE table_name = 'Table_Source'), (SELECT MAX(last_update) FROM Table_Source WHERE table_name = 'Table_Source'), @Date)
END;
注意:
- 这是插入我的数据Data Inserting Flow的流程
- 我不希望这种逻辑使用
NOT IN或NOT EXISTS查询,因为在我的情况下,我有来自Table_Source和Table_Destination的1000 万行。因为它会让我的数据库工作太辛苦。
【问题讨论】:
-
你好,我只是想补充一点:你应该使用
SELECT ts.*而不是SELECT *
标签: sql sql-server date sql-server-2008 duplicates