Hi all, I have a update statement I just can't seem to sort out. I'm a sql server native so this has been a struggle. I did some searching for similar statements and the syntax for doing it but haven't found much.
Basically I have to update records based on duplicate values found. The actual statement isn't that different to query but the update is killing me.
Here is what I have
I'm still reading up the documentation on DB2 so hopefully I'll find an answer but any input would be very appreciated if you see a big shinny mistake (which I'm sure there is)
A working T-SQL version is
____________ signature below ______________
You are a amateur developer until you realize all your code sucks.
Jeff Atwood
Basically I have to update records based on duplicate values found. The actual statement isn't that different to query but the update is killing me.
Here is what I have
Code:
UPDATE vendor_cost vc
INNER JOIN
(
SELECT
v_id,vi_id,cost_zone, store_id,MAX(vc_start_date) AS MAX_vc_start_date
FROM vendor_cost
WHERE vc_end_date is NULL
GROUP BY v_id, vi_id, cost_zone, store_id
HAVING COUNT(v_id) > 1
) A
INNER JOIN vendor_cost vc
ON A.v_id = vc.v_id
AND A.vi_id = vc.vi_id
AND A.cost_zone = vc.cost_zone
AND A.store_id = vc.store_id
SET vc_end_date = A.MAX_vc_start_date - 1 Day
WHERE
vc_end_date is NULL
AND
vc.vc_start_date < A.MAX_vc_start_date
I'm still reading up the documentation on DB2 so hopefully I'll find an answer but any input would be very appreciated if you see a big shinny mistake (which I'm sure there is)
A working T-SQL version is
Code:
UPDATE dbo.vendor_cost
SET vc_end_date = A.MAX_vc_start_date-1
FROM dbo.vendor_cost vc
INNER JOIN (
SELECT
v_id,vi_id,cost_zone,
store_id,
MAX(vc_start_date) AS MAX_vc_start_date
FROM dbo.vendor_cost
where vc_end_date is null
group by v_id, vi_id, cost_zone, store_id
having count(v_id) > 1
) A
on A.v_id = vc.v_id and A.vi_id = vc.vi_id and
A.cost_zone = vc.cost_zone and A.store_id = vc.store_id
where vc_end_date is null
and vc.vc_start_date <> A.MAX_vc_start_date
____________ signature below ______________
You are a amateur developer until you realize all your code sucks.
Jeff Atwood