WITH chunk(id,property,a,b,c,combined) AS
(SEL id,
property,
STRTOK(property, ',', 1) AS a,
STRTOK(property, ',', 2) AS b,
STRTOK(property, ',', 3) AS c,
CASE WHEN SUBSTRING(TRIM(a),1,5)=SUBSTRING(TRIM(b),1,5) AND SUBSTRING(TRIM(a),1,5) <>SUBSTRING(TRIM(c),1,5)
THEN SUBSTRING(TRIM(a),1,10) || ' ' || SUBSTRING(TRIM(b),8,3)||')'||','||c||','
WHEN SUBSTRING(TRIM(a),1,5)=SUBSTRING(TRIM(c),1,5) AND SUBSTRING(TRIM(a),1,5)<>SUBSTRING(TRIM(b),1,5)
THEN SUBSTRING(TRIM(a),1,10) || ' ' || SUBSTRING(TRIM(c),8,3)||')' ||','||TRIM(b)||','
WHEN SUBSTRING(TRIM(a),1,5)=SUBSTRING(TRIM(b),1,5) AND SUBSTRING(TRIM(a),1,5)=SUBSTRING(TRIM(c),1,5)
THEN SUBSTRING(TRIM(a),1,10) || ' ' || SUBSTRING(TRIM(b),8,3)||' '||SUBSTRING(TRIM(c),8,4)||','
ELSE 'Condition failed'
END AS combined
FROM Table2)
SELECT id,combined FROM chunk
ORDER BY id ;
输出#
id combined
1 1234X (Yel Gre),2345Y (Red),
2 2222Y (Pin Red Gre),
3 3454E (Yel Red),4565Y (Whi),
动态方式:
Is it possible to group string within a string in Teradata?
修改#
SEL id
, property
, SUBSTRING (property FROM 1 FOR INSTR(property, ',', 1, 1)-1 ) AS a
, SUBSTRING (property FROM INSTR(property, ',', 1, 1)+1 FOR INSTR(property, ',', 1, 2) -INSTR(property, ',', 1, 1)-1 ) AS b
, SUBSTRING (property FROM INSTR(property, ',', 1, 2)+1 FOR INSTR(property, ',', 1, 3) -INSTR(property, ',', 1, 2)-1 ) AS c
,CASE
WHEN SUBSTRING(TRIM(a),1,5)=SUBSTRING(TRIM(b),1,5) AND SUBSTRING(TRIM(a),1,5)<>SUBSTRING(TRIM(c),1,5)
THEN SUBSTRING(TRIM(a),1,10) || ' ' || SUBSTRING(TRIM(b),8,3)||')'||','||c||','
WHEN SUBSTRING(TRIM(a),1,5)=SUBSTRING(TRIM(c),1,5) AND SUBSTRING(TRIM(a),1,5)<>SUBSTRING(TRIM(b),1,5)
THEN SUBSTRING(TRIM(a),1,10) || ' ' || SUBSTRING(TRIM(c),8,3)||')' ||','||TRIM(b)||','
WHEN SUBSTRING(TRIM(a),1,5)=SUBSTRING(TRIM(b),1,5) AND SUBSTRING(TRIM(a),1,5)=SUBSTRING(TRIM(c),1,5)
THEN SUBSTRING(TRIM(a),1,10) || ' ' || SUBSTRING(TRIM(b),8,3)||' '||SUBSTRING(TRIM(c),8,4)||','
ELSE 'Condition failed'
END AS combined
FROM table2 ;