1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| DECLARE GLOBAL TEMPORARY TABLE SESSION.TMP_COMB
(
TMP_CO_CATEG Char(4)
, TMP_CO_GAR Char(4)
, TMP_RANG_GAR Integer
)
ON COMMIT PRESERVE ROWS ;
COMMIT ;
INSERT INTO SESSION.TMP_COMB VALUES ('CAT1' , 'GAR1' , '1');
INSERT INTO SESSION.TMP_COMB VALUES ('CAT1' , 'GAR2' , '2');
INSERT INTO SESSION.TMP_COMB VALUES ('CAT1' , 'GAR3' , '3');
INSERT INTO SESSION.TMP_COMB VALUES ('CAT2' , 'GAR1' , '1');
INSERT INTO SESSION.TMP_COMB VALUES ('CAT3' , 'GAR1' , '1');
INSERT INTO SESSION.TMP_COMB VALUES ('CAT3' , 'GAR2' , '2');
SELECT * FROM SESSION.TMP_COMB
with ur;
WITH
phrases (phrase, CO_CATEG, RANG_GAR)
AS
(
SELECT cast ( trim(TMP_CO_GAR) as varchar(200) ) concat ';'
, TMP_CO_CATEG
, TMP_RANG_GAR
FROM SESSION.TMP_COMB
WHERE TMP_RANG_GAR = 1
UNION ALL
SELECT phrase concat cast(trim(TMP_CO_GAR) as varchar(200) ) concat ';' as phrase
,TMP_CO_CATEG
,TMP_RANG_GAR
FROM SESSION.TMP_COMB AS suiv
INNER JOIN phrases
ON suiv.TMP_CO_CATEG = phrases.CO_CATEG
AND suiv.TMP_RANG_GAR = phrases.RANG_GAR + 1
)
, maxphrase
AS
(
SELECT CO_CATEG, MAX(RANG_GAR) AS maxposition
FROM phrases
GROUP BY CO_CATEG
)
SELECT P.CO_CATEG, PHRASE
FROM phrases AS P
INNER JOIN maxphrase AS M
ON P.CO_CATEG = M.CO_CATEG
AND P.RANG_GAR = M.maxposition
; |
Partager