Select only one distinct column
I guess that is an easy question to solve, but I'm researching for an hour for it's answer. I have a table with three columns: "id","id_x" and "name". But it's populated with repeated values, like this:
id id_x name
1 100 Name_aaa
2 100 Name_aaa
3 100 Name_aaa
4 100 Name_aaa*
5 101 Name_bbb
6 101 Name_bbb*
Ok, ok, I didn't create this table, and I can't modify them... I just wanna know what query I can execute to return only the values "100 - Name_aaa" and "101 - name_bbb"... Note that has an "*" after some names. I'd like to group only by the "id_x".
Is there any way to do it without 开发者_如何学Gousing subqueries or joins?
Thanks in advance!
Somewhat untested, but this should do it
select id_x, min(name) as name
from table
group by id_x
Try this,
select distinct id_x , name from tablename
select distinct
id_x,
case when name ~ E'\\*$' then substring(name, 1, length(name)-1)
else name end
from t;
Or:
SELECT DISTINCT ON (id_x) id_x, name FROM table ORDER BY id_x, name;
精彩评论