Can this be done in one query?

Поиск
Список
Период
Сортировка
Искать

Can this be done in one query?

От:
Randall Perry <rgp@systame.com>
Дата:
With the following:

    select max(date) from log order by date_time desc limit 12;

The limit clause has no effect on the results; it'll get the latest date
from the entire record set rather than from the last 12 records.

It works if I do this:
   create temp table as select date from log order by dat_time desc limit
12;

    select max(date) from temp;

So, is there a way to do this in one query without creating a temp table?

-- 
Randy Perry
sysTame
Mac Consulting/Sales

phn                 772.589.6449
mobile email        help@systame.com


Re: Can this be done in one query?

От:
"Bjoern Metzdorf" <bm@turtle-entertainment.de>
Дата:
> So, is there a way to do this in one query without creating a temp table?

How about a view:

create view test as select max(date) from log order by date_time desc limit
12;

select max(date) from test;

Greetings,
Bjoern


Re: Can this be done in one query?

От:
Stephan Szabo <sszabo@megazone23.bigpanda.com>
Дата:

On Tue, 7 May 2002, Randall Perry wrote:

> With the following:
>
>     select max(date) from log order by date_time desc limit 12;
>
> The limit clause has no effect on the results; it'll get the latest date
> from the entire record set rather than from the last 12 records.

Maybe something like:
select max(date) from (select date from log order by date_time desc limit
12) c;

Re: Can this be done in one query?

От:
Jean-Luc Lachance <jllachan@nsd.ca>
Дата:
Or maybe if date_time is of type timestamp:

select date( select date_time from log order by date_time desc limit 1);

JLL

Stephan Szabo wrote:
> 
> On Tue, 7 May 2002, Randall Perry wrote:
> 
> > With the following:
> >
> >     select max(date) from log order by date_time desc limit 12;
> >
> > The limit clause has no effect on the results; it'll get the latest date
> > from the entire record set rather than from the last 12 records.
> 
> Maybe something like:
> select max(date) from (select date from log order by date_time desc limit
> 12) c;
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
> http://archives.postgresql.org

Re: Can this be done in one query?

От:
Randall Perry <rgp@systame.com>
Дата:
Thanks, that's the best solution.

But I don't get the syntax; what's the 'c' at the end -- an alias?

> 
> On Tue, 7 May 2002, Randall Perry wrote:
> 
>> With the following:
>> 
>>     select max(date) from log order by date_time desc limit 12;
>> 
>> The limit clause has no effect on the results; it'll get the latest date
>> from the entire record set rather than from the last 12 records.
> 
> Maybe something like:
> select max(date) from (select date from log order by date_time desc limit
> 12) c;
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
> http://archives.postgresql.org

-- 
Randy Perry
sysTame
Mac Consulting/Sales

phn                 772.589.6449
mobile email        help@systame.com


Re: Can this be done in one query?

От:
Randall Perry <rgp@systame.com>
Дата:


> 
> On Tue, 7 May 2002, Randall Perry wrote:
> 
>> With the following:
>> 
>>     select max(date) from log order by date_time desc limit 12;
>> 
>> The limit clause has no effect on the results; it'll get the latest date
>> from the entire record set rather than from the last 12 records.
> 
> Maybe something like:
> select max(date) from (select date from log order by date_time desc limit
> 12) c;

Thanks, that did it.


> 
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
> http://archives.postgresql.org

-- 
Randy Perry
sysTame
Mac Consulting/Sales

phn                 772.589.6449
mobile email        help@systame.com


FAQ