简体   繁体   中英

Combining values from one column by the key value from another column

I need to combine all values by one column depends on the key from another column. Can someone help me to get out of this problem please?

here is the short example of my problem.

CUST_ID      CUST_REL_ID
100          1
100          2
100          3
100          4
200          5
200          6
200          7

CUST_ID      CUST_REL_ID
1            1
1            2
1            3
1            4
2            1
2            2
2            3
2            4
...
5            5
5            6
5            7

That's a simple join:

select t1.cust_id cust_id1, t1.cust_rel_id, t2.cust_id cust_id2
from table1 t1
inner join table2 t2 on t1.cust_rel_id = t2.cust_rel_id

I think you just want a self-join:

select t1.cust_rel_id, t2.cust_rel_id
from t t1 join
     t t2
     on t1.cust_id = t2.cust_id
order by t1.cust_rel_id, t2.cust_rel_id;

I don't understand your naming conventions. The column called cust_id in the result set looks nothing like the column called cust_id in the source data. But this appears to be what you want to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM