2

我对SQL相当陌生,现在尝试解决这个问题大约两天,但无济于事。

问题是,如何从 Table_2 中的值更新表 Table_1 中的值,只考虑 Table_2 中特定行的子集,并且尽可能不使用游标。

更具体地说,我有这两个表:

CREATE TABLE [dbo].[Table_1](
    [ID] [int] NOT NULL,
    [LastAmount] [int] NOT NULL,
    [LastDate] [datetime] NOT NULL
) ON [PRIMARY];

CREATE TABLE [dbo].[Table_2](
    [ID] [int] NOT NULL,
    [Amount] [int] NOT NULL,
    [Date_] [datetime] NOT NULL
) ON [PRIMARY];

表中有以下值:

INSERT INTO [dbo].[Table_1]
    VALUES (1, 0, CONVERT(DATETIME, '19000101', 112)),
           (2, 0, CONVERT(DATETIME, '19000101', 112));

INSERT INTO [dbo].[Table_2]
    VALUES (1, 10, CONVERT(DATETIME, '19750101', 112)),
           (1, 20, CONVERT(DATETIME, '19500101', 112)),
           (1, 15, CONVERT(DATETIME, '20000101', 112)),
           (2, 30, CONVERT(DATETIME, '20100101', 112));

重点是更新 ID 与 Table_2 匹配的 Table_1 中的值。Table_1.LastAmount 应该得到 Table_2.Amount 与最近的 Table_2.Date_。同样对于 Table_1.LastDate 它应该得到 Table_2.Date_ 其中日期是该特定 ID 的最新日期。

因此,更新前的 Table_1:

ID  |LastAmount |LastDate
----|-----------|--------
1   |0          |1900-01-01 00:00:00.0000
2   |0          |1900-01-01 00:00:00.0000

表_2:

ID  |Amount     |Date
----|-----------|--------
1   |10         |1975-01-01 00:00:00.0000
1   |20         |1950-01-01 00:00:00.0000
1   |15         |2000-01-01 00:00:00.0000
2   |30         |2010-01-01 00:00:00.0000

更新后的表_1:

ID  |LastAmount |LastDate
----|-----------|--------
1   |15         |2000-01-01 00:00:00.0000
2   |30         |2010-01-01 00:00:00.0000

我尝试使用 INNER JOIN 进行各种 UPDATE,或者在分配值时使用内联 SELECT,但它们都不起作用。提前谢谢了。

4

3 回答 3

0

这样做:

Update Table_1
Set LastAmount = Table_2.Amount,
LastDate = Table_2.[Date_]
from (SELECT ID, MAX([Date_]) as MaxDate FROM TABLE_2 GROUP BY ID) AS Max_Table2
inner join Table_2 on Max_Table2.MaxDate = Table_2.[Date_] and Max_Table2.id = Table_2.id
inner join table_1 on table_1.id = Table_2.id

基本上,您已经过滤了 table2 以获取最大日期并加入所有内容以查找最后日期和金额。

于 2013-06-07T13:30:48.960 回答
0

这是一个简单的使用ROW_NUMBER()和 CTE 来做到这一点:

;With Ordered as (
    select ID,Amount,Date_,
       ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Date_ DESC) rn
    from Table_2
)
update t1
set LastAmount = o.Amount,
LastDate = o.Date_
from
  Table_1 t1
    inner join
  Ordered o
    on
      t1.ID = o.ID and
      o.rn = 1

(您可以将其编写为子查询而不是 CTE,但我在此示例中选择了 CTE)

于 2013-06-07T13:42:05.077 回答
0

Anupdate from在这种情况下很有用。row_number()此处与源表一起用于确定Table_2.

update
    Table_1
set
    LastAmount = T2.Amount
    ,LastDate = T2.Date_
from
    (
        select
            ID
            ,Amount
            ,Date_
            ,row_number() over (partition by ID order by Date_ desc) as RowNumber
        from
            Table_2
    ) T2
where
    Table_1.ID = T2.ID
    and T2.RowNumber = 1
于 2013-06-07T13:42:49.387 回答