[英]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. 后台:使用OLEDB驱动程序连接到VFP数据库。 Scripting: php
脚本: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 选择partno,max(purch_date)为lastest_purch_date,价格FROM表GROUP BY partno
this query will not work because VFP will require an aggregate function for all fields selected. 此查询将不起作用,因为VFP将需要所选字段的聚合函数。 what im missing is a function or someway to tell vfp that i want the price corresponding to max(purch_date)
我缺少的是一个函数或者告诉vfp我想要的价格对应于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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.