Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL command: syntax needed
Message
From
22/03/2004 11:07:51
 
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00888526
Message ID:
00888541
Views:
10
>>I need a query to extract the most recent price inserted for a product
>>i.e.
>>
>>
>>id_product    year   price
>>1             2004   12004
>>1             2003   12003
>>2             2003   22003
>>2             2002   22002
>>
>>
>>so as to obtain
>>
>>id_product    year   price
>>1             2004   12004
>>2             2003   22003
>>
>>
>>
>>Which is the SQL syntax to perform the Select?
>>TIA
>
>SELECT TOP 1 price FROM {table} ORDER BY year DESC

Hmmm...that wouldn't work, that would only give you the very first one. You'd need a group by clause to make it work properly. Here's an example using the Northwind.Orders table:

select freight, orderdate from orders
where orderid in
(select min(orderid) from orders
group by datepart(yy,orderdate))
Dan LeClair
www.cyberwombat.com
SET RANT ON - The Wombat Blog

Life isn’t a morality contest and purity makes a poor shield. - J. Peter Mulhern
Disclaimer: The comments made here are only my OPINIONS on various aspects of VFP, SQL Server, VS.NET, systems development, or life in general, and my OPINIONS should not be construed to be the authoritative word on any subject. No warranties or degrees of veracity are expressed or implied. Void where prohibited. Side effects may included dizziness, spontaneous combustion, or unexplainable cravings for dark beer. Wash with like colors only, serve immediately for best flavor.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform