Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

sql - Get rid from one column duplicate values in two column select

So, I`ve got two columns t1.NAME and t2.ITEMS, for each neme there can be more than one item assigned to it, so I want to select it like:

| NAME | ITEMS |
  JOHN    1
          2
  BEN     4
          7
          3
  DAVE    5

P.s. if it helps, they are connected by t1.id = t2.names_id

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This kind of operation should be done in presentation layer.

enter image description here enter image description here

But if you insist you can use sth like:

SqlFiddleDemo

SELECT DISTINCT NAME,
      LISTAGG(Items,  chr(13)||chr(10)) WITHIN GROUP (ORDER BY 1) OVER (PARTITION BY Name) AS Items
FROM tab

change tab with subquery that produce output you get now.

The clue is to concatenate for every name corresponding Items and adding new line character CHR(13) + CHR(10).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...