简体   繁体   中英

Add to each row values from all other rows. sql

I got the table

ID  
1   
2   
3   
4   

I want to get table like that.

ID  Values
1   234
2   134
3   124
4   123

What i am trying to do is to add to each row ID from all other rows.

I tried to use except or not exists method but it doesn't work.

union_poly AS (
    SELECT b.id FROM poly b
    EXCEPT (SELECT * FROM poly ct)
    --WHERE NOT EXISTS(SELECT * FROM poly ct WHERE b.id <> ct.id)

  )

You can do:

select id,
       (select string_agg(t2.id::text, '' order by t2.id)
        from t t2
        where t2.id <> t.id
       ) as ids
from t;

Here is a Rextester. And a SQL Fiddle (it is rather slow these days).

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