如何使用另一个表中的max row no record更新表数据?
我的表格是这样的:
表1:
| ID | Client | Some_Value |Row_No |
| 1 | HP | 123 | 1 |
| 1 | HP | 1245 | 2 |
| 1 | Dell | 123445 | 3 |
| 2 | HP | 111 | 1 |
| 2 | HP | 223 | 2 |
| 3 | Dell | 34 | 1 |
| 3 | Dell | 5563 | 2 |
表2:
| ID X1 X2 Client | Some_Value |
| 1 ABCD 55 | |
| 1 ad 12 | |
| 2 asd 1234 | |
| 2 asd 34 | |
| 3 asd 2345 | |
| 3 ads 45 | |
现在我需要输出如下:
表2:
| ID X1 X2 Client |Some_Value |
| 1 ABCD 55 Dell | 123445 |
| 1 ad 12 Dell | 123445 |
| 2 asd 1234 HP | 223 |
| 2 asd 34 HP | 223 |
| 3 asd 2345 Dell | 5563 |
| 3 ads 45 Dell | 5563 |
答案 0 :(得分:0)
使用ROW_NUMBER
,然后加入您的表格:
WITH CTE AS
(
SELECT *,
RN = ROW_NUMBER() OVER(PARTITION BY ID ORDER BY Row_No DESC)
FROM Table1
)
UPDATE t2
SET t2.Some_Value = t1.Some_Value
FROM Table2 t2
INNER JOIN CTE t1
ON t2.ID = t1.ID
WHERE t1.RN = 1;
答案 1 :(得分:-1)
您可以使用查询,
select t1.ID,t2.X1,t2.X2,t1.Client,t1.Some_value from table1 t1
inner join table2 t2 on
t1.ID=t2.ID