For V value it is bringing two records inspite using distinctSorry, where are you using DISTINCT? I don't see anywhere in the code that you posted that you are doing a DISTINCT.
101 APPEAL 101 APPEAL 101 ORD RESP 101 ORD RESP 101 ORDER 101 ORDERI'm guessing that this is supposed to represent 6 rows in the base table. And I'm guessing that you want to reduce that to 3 rows and then pivot those rows. If so
If that is not what you want, please post the DDL to create your tables, the DML to populate them with sample data, and post the output that you expect.
INSERT INTO OUT_TABLE (col1,col2,col2,...) select * from ( SELECT K, V, ROW_NUMBER() OVER(PARTITION BY K ORDER BY V) RN FROM (SELECT DISTINCT k, v FROM DATA) ) pivot(max(v) col for rn in(1,2,3,4,5,6,7,8,9));