From: | Madison Kelly <linux(at)alteeve(dot)com> |
---|---|
To: | Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: SELECT DISTINCT ... ORDER BY problem |
Date: | 2008-12-09 20:59:02 |
Message-ID: | 493EDC16.8040800@alteeve.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Grzegorz Jaśkiewicz wrote:
> On Mon, Dec 8, 2008 at 10:19 PM, Madison Kelly <linux(at)alteeve(dot)com> wrote:
>> Hi all,
>>
>> I've got a table that I am trying to SELECT DISTINCT on one column and
>> ORDER BY on a second column, but am getting the error:
>>
>> SELECT DISTINCT ON expressions must match initial ORDER BY expressions
>
> try
> SELECT distinct, array_accum(bar) FROM table WHERE bar < '2008-12-07
> 16:32:46' AND tbl_id=153 ORDER BY bar LIMIT 1;
>
> or even, when you change bar to proper type - that is, timestamp
>
> SELECT distinct foo, min(bar) as minbar, max(bar) as maxbar FROM
> table WHERE bar < '2008-12-07
> 16:32:46' AND tbl_id=153 ORDER BY bar LIMIT 1;
>
> etc.
>
> Trick, is to use aggregate on other value(s).
>
> HTH
Well shoot, I didn't realize I had the 'text' where I should have used
'timestamp'. >_<
I updated the column, but it took me some fiddling (on a test box!) to
sort out the proper command. In case it helps someone else, here was the
error I was getting when I tried ALTER without USING:
ALTER TABLE table ALTER foo TYPE TIMESTAMP WITHOUT TIME ZONE;
ERROR: column "foo" cannot be cast to type "pg_catalog.timestamp"
The syntax I needed was:
ALTER TABLE table ALTER foo TYPE TIMESTAMP WITHOUT TIME ZONE USING CAST
(foo AS TIMESTAMP);
I know it's a little off-topic, but maybe it'll help someone searching
someday. :)
When I try to use:
SELECT distinct foo, min(bar) as minbar, max(bar) as maxbar FROM table
WHERE bar < '2008-12-07 16:32:46' AND tbl_id=153 ORDER BY bar LIMIT 1;
I get the error:
ERROR: column "table.foo" must appear in the GROUP BY clause or be used
in an aggregate function
Already a very big help though, thanks!
Madi
From | Date | Subject | |
---|---|---|---|
Next Message | Madison Kelly | 2008-12-09 21:16:22 | Re: SELECT DISTINCT ... ORDER BY problem |
Previous Message | Pavan Deolasee | 2008-12-09 18:09:40 | Re: tune postgres for UPDATE |