Search Postgresql Archives

Re: using window-functions to get freshest value - how?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In response to Massa, Harald Armin :
> > > Is it possible? How would the SQL utilizing WINDOW-functions look like?
> >
> > there is no point in using window functions in here - simply use
> > "DISTINCT ON".
> 
> and how would I use DISTINCT ON for this query? Please bear in mind,
> that there is more then one id_bf (just stopped the sample data with
> one of them)

For my example:

test=# select * from harald order by 1,2;
 g |   datum    | value
---+------------+-------
 1 | 2009-11-01 |    10
 1 | 2009-11-02 |    11
 1 | 2009-11-03 |    12
 1 | 2009-11-05 |     1
 2 | 2009-11-04 |    20
 2 | 2009-11-10 |   100
(6 rows)


the solution with DISTINCT ON:

test=*# select distinct on (g) * from harald order by g, datum desc;
 g |   datum    | value
---+------------+-------
 1 | 2009-11-05 |     1
 2 | 2009-11-10 |   100
(2 rows)


Regards, Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431  2EB0 389D 1DC2 3172 0C99

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux