Query to conditionally distribute rows to column

Oracle Database 10g Release 10.2.0.4.0 , working with Oracle SQL Developer

TableA – Key1 – Key2 – ConditionColumn

TA – K1 – K2 – C1

Query-1
SELECT K1, K2 FROM TA WHERE C1 = 1234 ORDER BY K1;

--K1 K2
--T40105 136853
--T40108 136861
--T40109 136869
--T40111 136878
--T40113 136886

TableB – Key2 – Data1 – Data2

TB – K2 – D1 – D2

Query-2
SELECT K2, D1, D2 FROM TB WHERE K2 = (SELECT K2 FROM TA WHERE K1='T40105');

--K2 D1 D2
--136853 1 1554
--136853 2 1552
--136853 3 1552

D1 is always = {1, 2, 3} for specific Key2.
How to combine these 2 Queries to get output as shown below for all values of K1?

--K1 K2 D2(@D1=1) D2(@D1=2) D2(@D1=3)
--T40105 136853 1554 1552 1552
--T40108 136861
--T40109 136869
--T40111 136878
--T40113 136886


Source: oracle

Leave a Reply