Possible sybase sql query -
i'm trying sql after many years away. i'm new sybase well. can please suggest possible query following problem?
there table called vegetables follows.
| product | price | date | | beans | 1.78 | 20040903 | | beans | 1.79 | 20040902 | | potato | 1.78 | 20040902 |
i need latest available prices each vegetable. intended database sybase. many thanks.
you can use subquery max(date)
each product
, join table:
select v1.product, v1.price, v1.date vegetables v1 inner join ( select product, max(date) maxdate vegetables group product ) v2 on v1.product = v2.product , v1.date = v2.maxdate;
see sql fiddle demo (demo sql server syntax should valid).
if version of sybase supports windowing functions, can use following:
select product, price, date ( select product, price, date, row_number() over(partition product order date desc) rn vegetables ) v rn = 1;
see sql fiddle demo
Comments
Post a Comment