简体   繁体   中英

SQL query on VFP DB: how to get data from row corresponding to the grouping and aggregation of another

background: Using OLEDB driver to connect to a VFP database. Scripting: php

below is a sample set of data item purchases:

partno  purch_date  price
  A     04/02/2012    95
  A     04/01/2012   100
  B     02/28/2012    55
  B     03/15/2012    60

what i want to do is to build a select query that would get the prices of the latest purchase price based on the date. this sounds simple enough but i could not for the life of me figure out how to do this. it seems like there must be an aggregate function that i dont know about. im thinking it should be something like the following:

SELECT partno, max(purch_date) as lastest_purch_date, price FROM table GROUP BY partno

this query will not work because VFP will require an aggregate function for all fields selected. what im missing is a function or someway to tell vfp that i want the price corresponding to max(purch_date)

any help will be greatly appreciated. thanks.

Try this

select b.partno,b.purdate,c.price
from
(
select max(a.purdate) purdate,partno from table a
group by a.partno
)b,
table c
where b.partno=c.partno and b.purdate=c.purdate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM