我目前正在創建一個每 24 小時從公共資料源獲取資料的服務。資料的結構基本上是這樣的:
| 一個 | b | C | 進口日期 |
|---|---|---|---|
| 1 | 2 | 3 | 22 年 6 月 12 日 |
| 2 | 3 | 3 | 22 年 6 月 12 日 |
| 1 | 2 | 3 | 22 年 6 月 11 日 |
我只想擁有唯一值(忽略匯入日期),即類似這樣的東西。
| 一個 | b | C | 進口日期 |
|---|---|---|---|
| 1 | 2 | 3 | 22 年 6 月 12 日 |
| 2 | 3 | 3 | 22 年 6 月 12 日 |
我們洗掉舊的重復值的地方。
解決此問題的最佳方法是什么,以確保實際上沒有資料丟失,只有重復的值。
提前致謝!
uj5u.com熱心網友回復:
您可以使用 row_number() 視窗函式,該函式將在您不想重復的值中創建磁區,畢竟您可以使用 where 子句僅過濾第一次出現。
select a, b, c, importDate
from (
select a, b, c, importDate,
row_number() over(partition by a,b,c order by a desc) rn
from example
) a
where rn =1;
這是示例:https ://www.db-fiddle.com/f/3iryppZrysgCPkRVjpCKyM/0
uj5u.com熱心網友回復:
我認為首先將新資料匯入具有相同結構的單獨表中是最簡單的,比如說Import,然后merge將其匯入 YourTable.
該merge陳述句允許您查詢一條資料,并將其與現有表進行匹配。在單個陳述句中,您可以選擇更新(或跳過)現有行,并插入新行。
merge into YourTable t
using
(select * from Import) i
on (i.a = t.a and i.b = t.b and i.c = t.c) -- Or just the columns you want to match
when matched then
update set t.ImportDate = i.ImportDate -- add any other columns you want to update
when not matched then
insert (a, b, c, ImportDate)
values (i.a, i.b, i.c, i.ImportDate);
uj5u.com熱心網友回復:
獲取所有資料后,在第二步中,我會執行類似的操作select a, b, c, max(ImportDate) as lastDate from source group by a, b, c,這應該將所有值保留為最后匯入的日期。
uj5u.com熱心網友回復:
您可以使用EXCEPT集合運算子找出差異。
declare @tgt table(a int, b int, c int);
;with src as
(
SELECT distinct * from
(values(1, 2, 3),(2, 3, 3),(5,6,7))as t(a,b,c)
), dst as
(
SELECT * from
(values(1, 2, 3),(2, 4, 5))as t(a,b,c)
)
select * from src
except
select * from dst
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/489452.html
