如何在 VFP v8.0 中根据另一个字段的最大值获取一个字段值

发布于 2024-08-31 18:00:57 字数 1102 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最单纯的乌龟 2024-09-07 18:00:57

DRapp 是正确的,因为如果 MeterNumber 上没有索引,则此查询会很慢。但是,如果您只想获取某一特定仪表的余额,则以下查询将返回相同的结果,并且速度应该更快。

SELECT ;
    MyTable.created, ;
    MyTable.balance ;
FROM ;
    MyTable ;
WHERE ;
    ( MyTable.meternumber = '2743876' ) ;
    AND ( MyTable.created IN (SELECT MAX(created) FROM MyTable WHERE (meternumber = '2743876')) ) ;

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.

SELECT ;
    MyTable.created, ;
    MyTable.balance ;
FROM ;
    MyTable ;
WHERE ;
    ( MyTable.meternumber = '2743876' ) ;
    AND ( MyTable.created IN (SELECT MAX(created) FROM MyTable WHERE (meternumber = '2743876')) ) ;
屌丝范 2024-09-07 18:00:57
select 
      YT.Balance,
      YT.Created
   from 
      YourTable YT
   where 
          YT.MeterNumber = '2743876'
      and YT.Created in
              ( select 
                      max( YT2.Created ) Created
                   from 
                      YourTable YT2
                   where 
                      YT.MeterNumber = YT2.MeterNumber 
              )

从 where 子句中删除第一个“YT.MeterNumber = '2743876' and”,您将获得所有仪表的所有余额...

此外,如果直接从 VFP 运行来测试,只需添加“;”每行末尾的续行。它应该与 VFP6、7、8 或 9 兼容。

根据您的其他评论,如果始终基于单个 MeterNumber 调用此查询,您可以调整为

select 
      YT.Balance,
      YT.Created
   from 
      YourTable YT,
      ( select YT2.MeterNumber,
               max( YT2.Created ) Created
           from 
               YourTable YT2
           where 
               YT2.MeterNumber = '2743876' 
           group by 
               1 ) YT3
   where 
          YT.MeterNumber = '2743876'
      and YT.MeterNumber = YT3.MeterNumber
      and YT.Created = YT3.Created
select 
      YT.Balance,
      YT.Created
   from 
      YourTable YT
   where 
          YT.MeterNumber = '2743876'
      and YT.Created in
              ( select 
                      max( YT2.Created ) Created
                   from 
                      YourTable YT2
                   where 
                      YT.MeterNumber = YT2.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

select 
      YT.Balance,
      YT.Created
   from 
      YourTable YT,
      ( select YT2.MeterNumber,
               max( YT2.Created ) Created
           from 
               YourTable YT2
           where 
               YT2.MeterNumber = '2743876' 
           group by 
               1 ) YT3
   where 
          YT.MeterNumber = '2743876'
      and YT.MeterNumber = YT3.MeterNumber
      and YT.Created = YT3.Created
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文