2017-10-26 2 views
0

ich ein Szenario wie dieses haben,aktualisieren gemeinsamen Inkrementwert auf einen Satz von doppelten Datensätzen

Col1 | Col2 | Col3 | Col4 
-------------------------- 
1 | null| Axy | Zcd 
2 | null| Axy | Zcd 
3 | null| Bxy | Yef 
4 | null| Bxy | Yef 
5 | null| Cvw | Dgh 

Ich versuche, Col2 zu aktualisieren, um einen gemeinsamen inkrementellen Wert zuzuweisen von Duplikaten zu setzen, versucht SQL mit auto- erhöhen, nachdem die Duplikate zu identifizieren, aber die Ausgaben sind jede Zeile Schritt mit anderem Wert erhält, ich diese Abfrage in DB2 LUW bin mit 9,7

update tab1 t1 
set t1.col2='A'|| seq_tab1.nextval --sequence 
    where exists (
    SELECT col2,trim(upper(col3)) col3,trim(upper(col4)) col4,col1 FROM tab1 t 
     where (col2 ='' or col2 is null) 
     and trim(upper(t1.col3)) = trim(upper(t.col3)) 
     AND trim(upper(t1.col4)) = trim(upper(t.col4)) 
     and t1.col1=t.col1 
     GROUP BY col2,trim(upper(col3)),trim(upper(col4)),col1 
     HAVING COUNT(*) >= 1) 
    and (t1.col2 ='' or t1.col2 is null); 

Ergebnis

Col1 | Col2 | Col3 | Col4 
-------------------------- 
1 | A1| Axy | Zcd 
2 | A2| Axy | Zcd 
3 | A3| Bxy | Yef 
4 | A4| Bxy | Yef 
5 | A5| Cvw | Dgh 

erwartete Ausgabe

Col1 | Col2 | Col3 | Col4 
-------------------------- 
1 | A1| Axy | Zcd 
2 | A1| Axy | Zcd 
3 | A2| Bxy | Yef 
4 | A2| Bxy | Yef 
5 | A3| Cvw | Dgh 

alle Empfehlungen ??

Antwort

0

diese Abfrage versucht, die ich in meinem Update versuchen

select col1,col2,col3,rn 
from (
select col1,col2,col3 
,dense_rank() over (order by col2 nulls last) as rn 
from tab1 
where (col1 ='' or col1 is null) 
) s1 
0

benutzten:

update tab1 f1            
set f1.new=(
select 'A' || f3.rang from 
      (         
      select f2.Col1, 
      dense_rank() over(order by f2.Col3, f2.Col4) rang           
      from tab1 f2 order by f2.Col1 
      ) f3           
where f1.Col1=f3.Col1       
)                
where exists             
(                
    select * from tab1 f2         
    where f1.Col1=f2.Col1      
) 
Verwandte Themen