简体   繁体   中英

SQL: use distinct values in one column to select all corresponding distinct values in another column

Suppose I have a data set such as this:

ColA    ColB
====    ====
Foo     Green
Foo     Purple
Bar     Magenta
Bar     Green
Foo     Purple
Bar     Purple
Bar     Purple
Foo     Green
Bar     Green

I would like to query this data and return the below. Essentially, take all distinct values from ColA (Foo & Bar) and return all distinct values for EACH of them from ColB:

ColA    ColB
====    ====
Foo     Green
Foo     Purple
Bar     Magenta
Bar     Green
Bar     Purple

I know I'm overthinking this...but I can't seem to hammer it out...

Please use of of the below -

SELECT DISTINCT cola,colb FROM yourtableName

OR

SELECT cola,colb From yourtableName
GROUP BY cola,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