简体   繁体   中英

How to extract non grouped column data?

I have a table A with following data:

A:

colA colB
 a     x
 b     x
 c     y
 d     y
 e     z
 f     z

I want the output as:

colA colA_1
 a     b
 c     d
 e     f

Ie I want to group the data based on colB and fetch the values from colA. I know that the same value will appear exactly twice in colB.

What I am trying to do is:

SELECT a1.colA, a2.colA
FROM A a1
JOIN A a2
ON a1.colA != a2.colA and a1.colB=a2.colB;

But this gives the output as:

colA colA_1
 a     b
 b     a
 c     d
 d     c
 e     f
 f     e

How can I fix this to get the desired output?

No need to join, simply do a GROUP BY :

SELECT min(colA), max(colA)
FROM A
group by colB

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