I am trying to select several columns from a table where one of the columns is unique. The select statement looks something like this:
select a, distinct b, c, d
from mytable
The table looks something like this:
| a | b | c | d | e |...
|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5
| 1 | 2 | 3 | 4 | 6
| 2 | 5 | 7 | 1 | 9
| 7 | 3 | 8 | 6 | 4
| 7 | 3 | 8 | 6 | 7
So the query should return something like this:
| a | b | c | d |
|---|---|---|---|
| 1 | 2 | 3 | 4
| 2 | 5 | 7 | 1
| 7 | 3 | 8 | 6
I just want to remove all of the rows where b is duplicated.
EDIT: There seems to be some confusion about which row I want to be selected in the case of duplicate b values. I don't care because the a, c, and d should (but are not guaranteed to) be the same.
You haven't said how to pick a row for each b value, but this will pick one for each.
You cannot put
DISTINCT
on a single column. You should put it right after theSELECT
:It return the result you need for your sample table. However if you require to remove duplicates only from a single column (which is not possible) you probably misunderstood something. Give us more descriptions and sample, and we try to guide you to the right direction.
I think you are nearly there with
DISTINCT
try:Try this
This will return what you're looking for but I think your example is flawed because you've no determinism over which value from the
e
column is returned.If you don't care what values you get for
B
,C
,D
, andE
, as long as they're appropriate for that key, you can group byA
:Note that
MAX()
would be just as valid. Some RDBMSs support aFIRST()
aggregate, or similar, for exactly these circumstances where you don't care which value you get (from a certain population).