简体   繁体   English

VFP DB上的SQL查询:如何从对应于另一行的分组和聚合的行获取数据

[英]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.

相关问题 SQL Server查询将行分组为列并获得相应的名称 - SQL Server query for grouping row as column and getting corresponding designation SQL查询和来自不同表的行数据分组 - SQL Query & Grouping Row Data From Different Tables 查询:如何从另一行获取数据以填充缺少的数据? - Query: How to get data from another row to fill lacking data? SQL查询从一个表中获取数据,而从另一表中仅获取一行数据 - SQL Query get data from one table, and get data from only one row from another table 如何使用Sql查询从数据行中获取完全匹配 - How to get exact match from data row using Sql query 如何从 sql 查询中获取上一个数据行? - how get previous data row from sql query? 关于如何从另一个表中获取随机记录到另一个表上的每一行的查询的 SQL 查询? - SQL query on how do I get a random record from another table into a query on another table for every row? 如何使用sql查询从另一个表中获取数据? - How to get data from another table using sql query? 在SQL中,如果一个表中没有相应的行,如何从一个表中删除一行? - In SQL, how to delete a row from one table if it doesn't have a corresponding row in another table? athena 上的复杂 SQL 查询聚合和分组 - Complex SQL query aggregation and grouping on athena
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM