sql - PostgreSQL GROUP BY LOWER() not working -
i trying use group by in postgresql 9.4.1 , not having success hoped.
there several folks on the web claim should work, can't same results. want case-insensitive group by every time add lower() complains with:
error: column "people.fn" must appear in group clause or used in aggregate function
create database test; create table people (id integer, fn text, ln text); /* id, firstname, lastname */ insert people (id, fn, ln) values(1,'mike','f'); insert people (id, fn, ln) values(2,'adam','b'); insert people (id, fn, ln) values(3,'bill','b'); insert people (id, fn, ln) values(4,'bill','r'); insert people (id, fn, ln) values(5,'mike','d'); insert people (id, fn, ln) values(6,'mike','c'); insert people (id, fn, ln) values(7,'mike','t'); select fn people group lower(fn); /* not run */ select fn people group fn; /* runs, not want */ here's get:
adam mike mike bill bill here's want:
mike adam bill obviously, there's i'm missing. , no, can't sanitize data put database. should read understand this?
generally, if want select in aggregate query, have group "something". in case, can results want selecting lower(fn):
select lower(fn) people group lower(fn) luckily, postgresql allows group alias, don't have repeat lower(fn) twice:
select lower(fn) lfn people group lfn as @okaram mentioned in comments, if don't need other aggrgation on table, you'd better use distinct:
select distinct lower(fn) people
Comments
Post a Comment