मुझे लगता है कि आपका तर्क merge . का उपयोग कर रहा है और row_number() सही रास्ते पर है (हालांकि, संभवतः, partition by क्लॉज की जरूरत नहीं है, क्योंकि आप पहले से ही शहर को छान रहे हैं)। मैंने तारीखों को संभालने के लिए इसे अतिरिक्त तर्क के साथ बढ़ाया:
-
आरंभिक
effective_dt_fromऔर अंतिमeffective_dt_toअछूता छोड़ दिया जाना चाहिए -
बीच में, आप
'2017-01-01'. से शुरू होने वाली तारीखों को दिन-ब-दिन बढ़ाना चाहते हैं ।
प्रश्न:
merge into test t
using (
select
t.*,
row_number() over(order by loc_sid asc) rn,
count(*) over() cnt
from test t
where city = 'Chicago'
) t1
on (t1.loc_sid = t.loc_id)
when matched the update set
t.version = t1.rn,
t.effective_dt_from =
case
when rn = 1 then t.effective_dt_from
else date '2017-01-01' + rn - 2
end,
t.effective_dt_to =
case
when rn = cnt then t.effective_dt_to
else date '2017-01-01' + rn - 1
end