Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Table 1 input:
id name
1 abc
2 xyz
3 abc
4 efg
5 abc
6 abc
Table 2 output:
id name
1 abc
2 xyz
3 abc#1
4 efg
5 abc#2
6 abc#3
select count(*) from table 2 where name regex '<current name>#+$'
package routines;
import java.util.HashMap;
import java.util.Map;
public class SNumeric {
private static final Map<String, Integer> seq_Hash = new HashMap<String, Integer>();
private static final Map<String, Map<String,Integer>> key_Hash = new HashMap<String, Map<String,Integer>>();
/**
* return the value corresponding to the key or an incremented numeric id
*
* {talendTypes} int | Integer
*
* {Category} Numeric
*
* {param} string("s1") sequence identifier
*
* {param} string("key") key
*
* {param} int(1) step
*
* {example} sequenceKey("s1", "abc", 1) # "abc", "abc#1", "abc#2", ...
*
* {example} sequenceKey("s1", "abc", 3) # "abc", "abc#3", "abc#6", ...
*
*/
public static String setUniqueId(String seqName, String key, int step) {
if (seq_Hash.containsKey(seqName)) {
if (key_Hash.get(seqName).containsKey(key)) {
return key + "#" + key_Hash.get(seqName).get(key);
} else {
Integer incValue = seq_Hash.get(seqName) + step;
seq_Hash.put(seqName, incValue);
key_Hash.get(seqName).put(key, incValue);
return key + "#" + incValue;
}
} else {
seq_Hash.put(seqName, startValue);
key_Hash.put(seqName, new HashMap<Object, Integer>());
return key;
}
}
}
sequence label id
seq abc 4
seq xyz 3
seq2 abc 1
id name count
1 abc 3
2 xyz 1
3 efg 1
|
v
----Iteration--->----Iteration--->----row--->----row--->
Table 1 input:
id name
1 abc
2 xyz
3 abc
4 efg
5 abc
6 abc
Table 2 output:
id name
1 abc
2 xyz
3 abc#1
4 efg
5 unique key error tried to use the name: abc#1
6 unique key error tried to use the name: abc#1