基于另一个行值更新行的值列

时间:2017-03-10 14:48:44

标签: sql sql-server-2008-r2

我有一个历史表,数据如下:

在我的情况下,DateTime(110,111,112)更改了product id,因为我拥有与price相同的产品,有时我可以使用相同的重复数据没有信息改变

product_updateDate

我想要的是进行更新查询以获得结果:

+------+------------+--------------+---------------+-----------------------------+-------------------------+-------------------------+----------------+
| id   | product_id | product_name | product_Price |       product_addDate       |   product_UpdateDate    |      Insertdate_DB      | Updatedate_DB  |
+------+------------+--------------+---------------+-----------------------------+-------------------------+-------------------------+----------------+
|    1 |        110 |    DELL      |          1000 |    2017-03-01 08:00:00.000  | NULL                    | 2017-03-06 10:00:00.000 | NULL           |
|    2 |        111 |    HP        |           900 |    2017-03-01 08:00:00.000  | NULL                    | 2017-03-06 10:00:00.000 | NULL           |
| 3    |        112 |    Mac       |          1300 |    2017-03-01 08:00:00.000  | NULL                    | 2017-03-06 10:00:00.000 | NULL           |
| 4    |        113 |   Lenovo     |           950 |    2017-03-01 08:00:00.000  | NULL                    | 2017-03-06 10:00:00.000 | NULL           |
| 5    |        110 |   DELL       |           900 |    2017-03-04 08:00:00.000  | 2017-03-04 08:00:00.000 | 2017-03-07 10:00:00.000 | NULL           |
| 6    |        111 |    HP        |           800 |    2017-03-04 08:00:00.000  | 2017-03-04 08:00:00.000 | 2017-03-07 10:00:00.000 | NULL           |
| 7    |        112 |    Mac       |           120 |    2017-03-04 08:00:00.000  | 2017-03-04 08:00:00.000 | 2017-03-07 10:00:00.000 | NULL           |
+------+------------+--------------+---------------+-----------------------------+-------------------------+-------------------------+----------------+

以下查询可以更新 +----+------------+--------------+---------------+-------------------------+-------------------------+-------------------------+---------------+ | id | product_id | product_name | product_Price | product_addDate | product_UpdateDate | Insertdate_DB | Updatedate_DB | +----+------------+--------------+---------------+-------------------------+-------------------------+-------------------------+---------------+ | 1 | 110 | DELL | 1000 | 2017-03-01 08:00:00.000 | 2017-03-04 08:00:00.000 | 2017-03-06 10:00:00.000 | GETDATE() | | 2 | 111 | HP | 900 | 2017-03-01 08:00:00.000 | 2017-03-04 08:00:00.000 | 2017-03-06 10:00:00.000 | GETDATE() | | 3 | 112 | Mac | 1300 | 2017-03-01 08:00:00.000 | 2017-03-04 08:00:00.000 | 2017-03-06 10:00:00.000 | GETDATE() | | 4 | 113 | Lenovo | 950 | 2017-03-01 08:00:00.000 | NULL | 2017-03-06 10:00:00.000 | NULL | | 5 | 110 | DELL | 900 | 2017-03-04 08:00:00.000 | NULL | 2017-03-07 10:00:00.000 | NULL | | 6 | 111 | HP | 800 | 2017-03-04 08:00:00.000 | NULL | 2017-03-07 10:00:00.000 | NULL | | 7 | 112 | Mac | 120 | 2017-03-04 08:00:00.000 | NULL | 2017-03-07 10:00:00.000 | NULL | +----+------------+--------------+---------------+-------------------------+-------------------------+-------------------------+---------------+

product_UpdateDate

但是,当我尝试更新此列时,我想更改update tablename set product_UpdateDate = case when tablename.product_UpdateDate is null then t.product_UpdateDate else null end from tablename join tablename t on tablename.product_id = t.product_id and t.ID <> tablename.id ` Updatedate_DB值,所有行的值都为GETDATE(),但我只想更改行的GETDATE()价值有变化

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我认为row_number()可能是一种更简单的方法。逻辑很难遵循,但以下内容应该会导致问题中的数据:

with toupdate as (
      select t.*,
             row_number() over (partition by product_id order by id) as seqnum,
             count(*) over (partition by product_id) as cnt
      from t
     )
update toupdate
    set product_UpdateDate = (case when seqnum = 1 then product_addDate end),
        Updatedate_DB = (case when seqnum = 1 then getdate() end)
    where cnt > 1;