I have a historical table with data as bellow :
In my case the product id
(110,111,112) had changed the price
that way i have the same product with the value of the product_updateDate
also sometimes i can have the same duplicate data with no information changed
+------+------------+--------------+---------------+-----------------------------+-------------------------+-------------------------+----------------+
| 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 |
+------+------------+--------------+---------------+-----------------------------+-------------------------+-------------------------+----------------+
What i want is doing an Update Query to get the result as bellow:
+----+------------+--------------+---------------+-------------------------+-------------------------+-------------------------+---------------+
| 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 |
+----+------------+--------------+---------------+-------------------------+-------------------------+-------------------------+---------------+
The query as bellow can update the 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 `
But also i want to change the Updatedate_DB
with the GETDATE()
value when i try to do update this column all rows have the value GETDATE()
but i want change only the rows that the value have change
Thanks for help
I am not sure I understood your question, but if you want to change only the row where you wrote a date (and not null), may be you could add this to your update:
, UPDATEDATE_DB=CASE WHEN TABLENAME.PRODUCT_UPDATEDATE IS NULL THEN 'GETDATE()'
ELSE TABLENAME.UPDATEDATE_DB END
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.