Code: |
Field1 Field2 ------------------ A 1 B 1 B 3 C 3 |
Code: |
CREATE TABLE MYSCHEMA.ORIGINAL (F1 SMALLINT, F2 CHAR(1)); CREATE TABLE MYSCHEMA.SUPPORT (F1 SMALLINT) ; INSERT INTO MYSCHEMA.SUPPORT VALUES (1); INSERT INTO MYSCHEMA.SUPPORT VALUES (2); INSERT INTO MYSCHEMA.SUPPORT VALUES (3); INSERT INTO MYSCHEMA.SUPPORT VALUES (4); INSERT INTO MYSCHEMA.SUPPORT VALUES (5); INSERT INTO MYSCHEMA.ORIGINAL VALUES (1,'A'); INSERT INTO MYSCHEMA.ORIGINAL VALUES (1,'B'); INSERT INTO MYSCHEMA.ORIGINAL VALUES (3,'B'); INSERT INTO MYSCHEMA.ORIGINAL VALUES (3,'C'); SELECT LISTAGG (CASE WHEN (T2.F1 IS NULL) THEN 'N' ELSE 'Y' END ) WITHIN GROUP (ORDER BY T1.F1 ASC) AS FLAGLIST FROM MYSCHEMA.SUPPORT T1 LEFT OUTER JOIN ( SELECT T3.F1 FROM MYSCHEMA.ORIGINAL T3 WHERE T3.F2 = 'C' ) T2 ON T1.F1 = T2.F1 |
Code: |
SELECT * FROM (SELECT CASE WHEN MAX(F1) = 1 THEN CHAR('YNNNN') WHEN MAX(F1) = 2 THEN CHAR('NYNNN') WHEN MAX(F1) = 3 THEN CHAR('NNYNN') WHEN MAX(F1) = 4 THEN CHAR('NNNYN') WHEN MAX(F1) = 5 THEN CHAR('NNNNY') ELSE CHAR('NNNNN') END AS SRCHSTR FROM MYSCHEMA.ORIGINAL GROUP BY F2) A WHERE A.SRCHSTR = 'YNNNN' ; |
Code: |
-- FLAGS FOR VALUE 'B' SELECT LISTAGG (CASE WHEN (T2.F1 IS NULL) THEN 'N' ELSE 'Y' END ) WITHIN GROUP (ORDER BY T1.F1 ASC) AS FLAGLIST FROM MYSCHEMA.SUPPORT T1 LEFT OUTER JOIN ( SELECT T3.F1 FROM MYSCHEMA.ORIGINAL T3 WHERE T3.F2 = 'B' ) T2 ON T1.F1 = T2.F1 ; |
Code: |
YNYNN |
Code: |
INSERT INTO MYSCHEMA.ORIGINAL VALUES (1,'A'); INSERT INTO MYSCHEMA.ORIGINAL VALUES (2,'A'); INSERT INTO MYSCHEMA.ORIGINAL VALUES (3,'A'); INSERT INTO MYSCHEMA.ORIGINAL VALUES (4,'A'); INSERT INTO MYSCHEMA.ORIGINAL VALUES (5,'A'); INSERT INTO MYSCHEMA.ORIGINAL VALUES (1,'B'); INSERT INTO MYSCHEMA.ORIGINAL VALUES (3,'B'); INSERT INTO MYSCHEMA.ORIGINAL VALUES (3,'C'); |
Code: |
YYYYY << "A" has all value 1 thru 5 YNYNN << "B" has 1 and 3 NNYNN << "C" has only 3 |
Code: |
SELECT REPLACE(REPLACE(SUBSTR(DIGITS(SUM(CASE WHEN F1 = 1 THEN SMALLINT(10000) WHEN F1 = 2 THEN SMALLINT(01000) WHEN F1 = 3 THEN SMALLINT(00100) WHEN F1 = 4 THEN SMALLINT(00010) WHEN F1 = 5 THEN SMALLINT(00001) ELSE SMALLINT(00000) END)),6,5),'1','Y'),'0','N') FROM MYSCHEMA.ORIGINAL GROUP BY F2 ; |
bauer wrote: |
kolusu,
yes, you are right about the expected result. Your idea with the SUM operator is great, looks pretty good. But: In my sample I used 5 digits, 5 possible values. The real SQL should handle 255 digits (maximum allowable char length in DB2 for non varying fields). Thus your CASE does not really look very nice. The performance might be much better than my solution with with additional table. But values from 1 to 255, even if I use big integer instead of small, I do not have enough digits. Nice task for the evening to think about the general approach. Thank you again for your time and providing a new idea. Bauer |
bauer wrote: |
But values from 1 to 255, even if I use big integer instead of small, I do not have enough digits. |
Code: |
SELECT CHAR(IFNULL (REPLACE(SUBSTR(A.MY_STR,1,A.LPAD_VAL) || CHAR('Y') || SUBSTR(A.MY_STR,A.RPOS,A.RPAD_VAL),' ','N'),' '),255) FROM (SELECT RPAD(' ',256,'N') AS MY_STR ,SMALLINT(254 - (255 - F1)) AS LPAD_VAL ,SMALLINT(255 - F1) AS RPAD_VAL ,SMALLINT(F1 +1 ) AS RPOS FROM MYSCHEMA.ORIGINAL) A |
bauer wrote: |
Hi kolusu,
my impression is: This does not work. See the discussed sample above: YNYNN << "B" has 1 and 3 How is this generated? The provided SQL pads from the left some 'N', one 'Y', and from the right some 'N'. Something like WHERE MySchema.ORIGINAL.F2 = <AnyValue> seems missing. Or did I miss something? bauer |
kolusu wrote: |
Note : This is not summarized version of the records, it is on individual record basis. The duplicates are not eliminated. |
Code: |
---------+-- F2 F1 ---------+-- A 1 A 2 A 3 A 4 A 5 A 255 B 1 B 5 C 4 D 6 D 256 << will be ignored. E 16 |
Code: |
F2 F1 SLOT FLAG_POS ---------+---------+---------+------- A 1 0 1 A 2 0 2 A 3 0 3 A 4 0 4 A 5 0 5 A 255 15 15 B 1 0 1 B 5 0 5 C 4 0 4 D 6 0 6 D 256 16 0 <<< ignored E 16 1 0 |
Code: |
SELECT CHAR(SUBSTR(IFNULL( REPLACE(REPLACE(SUBSTR(DIGITS(B.S00),4,16) || SUBSTR(DIGITS(B.S01),4,16) || SUBSTR(DIGITS(B.S02),4,16) || SUBSTR(DIGITS(B.S03),4,16) || SUBSTR(DIGITS(B.S04),4,16) || SUBSTR(DIGITS(B.S05),4,16) || SUBSTR(DIGITS(B.S06),4,16) || SUBSTR(DIGITS(B.S07),4,16) || SUBSTR(DIGITS(B.S08),4,16) || SUBSTR(DIGITS(B.S09),4,16) || SUBSTR(DIGITS(B.S10),4,16) || SUBSTR(DIGITS(B.S11),4,16) || SUBSTR(DIGITS(B.S12),4,16) || SUBSTR(DIGITS(B.S13),4,16) || SUBSTR(DIGITS(B.S14),4,16) || SUBSTR(DIGITS(B.S15),4,16) ,'1','Y') ,'0','N'),' '),2,255)) FROM( SELECT F2 ,SUM(CASE WHEN SLOT = 00 THEN C01 ELSE BIGINT(0) END) AS S00 ,SUM(CASE WHEN SLOT = 01 THEN C01 ELSE BIGINT(0) END) AS S01 ,SUM(CASE WHEN SLOT = 02 THEN C01 ELSE BIGINT(0) END) AS S02 ,SUM(CASE WHEN SLOT = 03 THEN C01 ELSE BIGINT(0) END) AS S03 ,SUM(CASE WHEN SLOT = 04 THEN C01 ELSE BIGINT(0) END) AS S04 ,SUM(CASE WHEN SLOT = 05 THEN C01 ELSE BIGINT(0) END) AS S05 ,SUM(CASE WHEN SLOT = 06 THEN C01 ELSE BIGINT(0) END) AS S06 ,SUM(CASE WHEN SLOT = 07 THEN C01 ELSE BIGINT(0) END) AS S07 ,SUM(CASE WHEN SLOT = 08 THEN C01 ELSE BIGINT(0) END) AS S08 ,SUM(CASE WHEN SLOT = 09 THEN C01 ELSE BIGINT(0) END) AS S09 ,SUM(CASE WHEN SLOT = 10 THEN C01 ELSE BIGINT(0) END) AS S10 ,SUM(CASE WHEN SLOT = 11 THEN C01 ELSE BIGINT(0) END) AS S11 ,SUM(CASE WHEN SLOT = 12 THEN C01 ELSE BIGINT(0) END) AS S12 ,SUM(CASE WHEN SLOT = 13 THEN C01 ELSE BIGINT(0) END) AS S13 ,SUM(CASE WHEN SLOT = 14 THEN C01 ELSE BIGINT(0) END) AS S14 ,SUM(CASE WHEN SLOT = 15 THEN C01 ELSE BIGINT(0) END) AS S15 FROM (SELECT F2 AS F2 ,INT(F1/16) AS SLOT ,CASE WHEN MOD(F1,16) = 00 THEN BIGINT(1000000000000000) WHEN MOD(F1,16) = 01 THEN BIGINT(0100000000000000) WHEN MOD(F1,16) = 02 THEN BIGINT(0010000000000000) WHEN MOD(F1,16) = 03 THEN BIGINT(0001000000000000) WHEN MOD(F1,16) = 04 THEN BIGINT(0000100000000000) WHEN MOD(F1,16) = 05 THEN BIGINT(0000010000000000) WHEN MOD(F1,16) = 06 THEN BIGINT(0000001000000000) WHEN MOD(F1,16) = 07 THEN BIGINT(0000000100000000) WHEN MOD(F1,16) = 08 THEN BIGINT(0000000010000000) WHEN MOD(F1,16) = 09 THEN BIGINT(0000000001000000) WHEN MOD(F1,16) = 10 THEN BIGINT(0000000000100000) WHEN MOD(F1,16) = 11 THEN BIGINT(0000000000010000) WHEN MOD(F1,16) = 12 THEN BIGINT(0000000000001000) WHEN MOD(F1,16) = 13 THEN BIGINT(0000000000000100) WHEN MOD(F1,16) = 14 THEN BIGINT(0000000000000010) WHEN MOD(F1,16) = 15 THEN BIGINT(0000000000000001) ELSE BIGINT(0) END AS C01 FROM MYSCHEMA.ORIGINAL) A GROUP BY A.F2) B ; |
bauer wrote: |
Thank you again for your time and "stressed convolutions of your brain". |
output generated using printer-friendly topic mod. All times are GMT - 5 Hours