简体   繁体   中英

How To Get A Field Value Based On The Max Of Another Field In VFP v8.0

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')) ) ;
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

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')) ) ;

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