我正在获取这样的数据
来自查询select distinct(trim(name)) as loc from locations order by trim(name)
现在,我想使用lower()或upper()删除重复项,然后选择第一条记录。 如果我使用如下所示的下级功能,
select distinct(lower(A.loc)) from( select distinct(trim(name)) as loc from locations order by trim(name) ) as A order by lower(A.loc);
它将结果转换为以下较低的值。
阿布扎比
但是我想要前面提到的原始结果。
答案 0 :(得分:1)
SELECT DISTINCT ON (lower(city))
city
FROM
cities
DISTINCT ON
占据任意列,并给出第一个重复项。在这种情况下,将在内部创建一个全为小写的列。然后,获取第一条记录,但仅获取原始列。
SELECT DISTINCT ON (lower(city))
city
FROM
cities
ORDER BY lower(city), city DESC
ORDER BY lower(city)
是必需的,因为DISTINCT ON
需要将给定的列排在第一位。之后,您可以按任何其他列进行订购。 ORDER BY column DESC
将大写字母移到顶部。