Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Select TOP 1 on two fields
Message
From
09/03/2023 08:56:42
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01686062
Message ID:
01686352
Views:
83
Hi Dmitry, from what you explained I think this will work:
SELECT a.DATE_FLD, a.ORDER_NO, a.FIELD2, a.FIELD3
   FROM MyTable a
      INNER JOIN
         (
         SELECT DATE_FLD, MAX(ORDER_NO) AS ORDER_NO
            FROM MyTable
            GROUP BY DATE_FLD
         ) b ON a.DATE_FLD = b.DATE_FLD AND a.ORDER_NO = b.ORDER_NO
   ORDER BY a.DATE_FLD, a.ORDER_NO
I hope this solves your needs.

Luis

>>>Hi,
>>>
>>>I am creating a SQL Select that will select ONE record based on two fields. The two fields are DATE_FLD and ORDER_NO field. There could be more than one records with the same DATE_FLD value (e.g. "01/01/2023) but the ORDER_NO will be different for each record. I need to select a record with the largest value in the ORDER_NO field.
>>>
>>>Here is my test:
>>>
>>>select TOP 1 CONVERT(VARCHAR(10),DATE_FLD,101) + STR(ORDER_NO), FIELD2, FIELD3 from MyTable order by DATE_FLD, ORDER_NO DESC
>>>
>>>
>>>Will the above give me what I am looking for?
>>>
>>>TIA
>>>
>>>UPDATE. I think the problem I have is defining the ORDER BY clause correctly. What am I missing?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform