如何在 VFP v8.0 中根据另一个字段的最大值获取一个字段值
因此,我有一个表,我想从记录中的一个字段获取值,该值在另一个字段中具有最大的 DateTime() 值,并且还有一个字段等于某个值。
示例数据:
Balance Created MeterNumber
7924.252 02/02/2010 10:31:48 AM 2743800
7924.243 02/02/2010 11:01:37 AM 2743876
7924.227 02/02/2010 03:55:50 PM 2743876
我想获取特定仪表编号具有最大创建日期时间的记录的余额。在 VFP 7 中,我可以使用:
SELECT a.balance ,MAX(a.created) FROM MyTable a WHERE a.meternumber = '2743876'
但是,在我在 ASP.NET 页面中使用的 VFP v8.0 OleDb 驱动程序中,我必须符合 VFP 8,其中规定必须有一个 GROUP BY 列出 SELECT 中列出的每个非聚合字段。如果我将 GROUP BY a.balance
添加到查询中,这将为每个余额返回一条记录。
是的,我可以发出SET ENGINEBEHAVIOR 70
,但我想知道这是否可以在不必恢复到以前版本的情况下完成?
编辑 我确实让 Frank Perez 查询正常工作,但只是在将 SQL IN 子句的日期时间字段转换为字符之后。请注意使用 TTOC() 函数对 DateTime 字段进行包装。
SELECT ;
MyTable.created, ;
MyTable.balance ;
FROM ;
MyTable ;
WHERE ;
( MyTable.meternumber = '2743876' ) ;
AND ( TTOC(MyTable.created) IN (SELECT TTOC(MAX(created)) FROM MyTable WHERE (meternumber = '2743876')) ) ;
So, I have a table and I want to get the value from one field in the record with the greatest DateTime() value in another field and where still another field is equal to a certain value.
Example data:
Balance Created MeterNumber
7924.252 02/02/2010 10:31:48 AM 2743800
7924.243 02/02/2010 11:01:37 AM 2743876
7924.227 02/02/2010 03:55:50 PM 2743876
I want to get the balance for a record with the greatest created datetime for a specific meter number. In VFP 7 I can use:
SELECT a.balance ,MAX(a.created) FROM MyTable a WHERE a.meternumber = '2743876'
But, in the VFP v8.0 OleDb driver I am using in my ASP.NET page I must conform to VFP 8 which says you must have a GROUP BY listing each non aggregate field listed in the SELECT. This would return a record for each balance if I added GROUP BY a.balance
to my query.
Yes, I could issue a SET ENGINEBEHAVIOR 70
but I wanted to know if this could be done without having to revert to a previous version?
EDIT
I did get Frank Perez query to work but only after converting the DateTime fields to Character for the SQL IN clause. Note the wrapping of the DateTime fields with the TTOC() function.
SELECT ;
MyTable.created, ;
MyTable.balance ;
FROM ;
MyTable ;
WHERE ;
( MyTable.meternumber = '2743876' ) ;
AND ( TTOC(MyTable.created) IN (SELECT TTOC(MAX(created)) FROM MyTable WHERE (meternumber = '2743876')) ) ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DRapp 是正确的,因为如果 MeterNumber 上没有索引,则此查询会很慢。但是,如果您只想获取某一特定仪表的余额,则以下查询将返回相同的结果,并且速度应该更快。
DRapp is correct in that if there is not an index on MeterNumber, this query will be slow. However, if you are only trying to get the balance for one particular meter, the following query will return the same results and should be quicker.
从 where 子句中删除第一个“YT.MeterNumber = '2743876' and”,您将获得所有仪表的所有余额...
此外,如果直接从 VFP 运行来测试,只需添加“;”每行末尾的续行。它应该与 VFP6、7、8 或 9 兼容。
根据您的其他评论,如果始终基于单个 MeterNumber 调用此查询,您可以调整为
Remove the first "YT.MeterNumber = '2743876' and" from the where clause and you'll get all balances for all meters...
Additionally, if run from VFP directly to test, just add ";" line continuation at the end of each line. It should be complient no problem with VFP6, 7, 8 or 9.
Per your other comments, if this query is always going to be called based on a SINGLE MeterNumber, you could adjust as