Solved

returning the row with the most current date in Informix/4gl

Posted on 2000-03-25
8
438 Views
Last Modified: 2012-08-14
I have to run a query where the chances of returning multiple rows for each individual item is very likely,


Item        Price Date
0123456789 $4.00 1999-07-01-99
0123456789 $3.00 1999-05-01-99

What function could I use to return the most recent value of the item only?

0123456789 price 1999-07-01-99



Thanks, Had(Mike)

0
Comment
Question by:hadrons
8 Comments
 
LVL 2

Expert Comment

by:JHausmann
ID: 2656995
select item, price, date from table where date in (select max(date) from table where item='0123456789') and item ='0123456789'
0
 

Expert Comment

by:lbrooks65
ID: 2657300
Use max() fuction and a correlated subquery.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2657967
I assume you work with SQLServer, on other DBMS the syntax TOP 1 may be replaced by something else:
So fastest should be:
select TOP 1 item, price, date from table where item='0123456789' and item ='0123456789'
order by date desc
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 6

Expert Comment

by:jpk041897
ID: 2659495
Depending on the DB you use, you can use one of two approaches:

1.- Use a phisical index, rather than a data index.

All databases maintain an internal physical index (record numner, sector/block, etc.) which you could use to determine the most recent entry (i.e.: the one with the higher physical index). Angel III's answer falls into this category.


2.- Use the time part of the date to determine most recent entry.

Provided you are storing the date in a type DATE column (and not as a string), the date column will also allow you to determine the most recent entry since it will store a time part as well as the date. This is the essence of JHaussmans answer.

Both solutions are correct, and both will work. As to determining which one to use though... :-)

Physical index will give you the fastest acces, but the solution is non portable and might require the use of a stored procedure depending on the DB you are using (Oracle for instance).

The date field solution is considerably slower and will probably require that you create a secondary index on the DATE field if you will be running the querry repeatedly.

Once you know the options the choice is up to you.
0
 
LVL 10

Expert Comment

by:paasky
ID: 2661110
Hello,

TOP 1 solution looks great. If TOP predicate is not available, here's my another suggestion:

SELECT Last(A1.Item), Last(A1.Price) , A1.Date
FROM PriceList AS A1
WHERE A1.Date=
   (SELECT MAX(A2.Date)
    FROM PriceList AS A2
    WHERE A1.Item = A2.Item)
GROUP BY A1.Date;

It will show only the last record if there are multiple records in price-list in same day.

Regards,
Paasky
0
 

Accepted Solution

by:
lbrooks65 earned 100 total points
ID: 2740388
Use max() fuction and a correlated subquery.
0
 
LVL 10

Expert Comment

by:paasky
ID: 2740588
Hello hadrons, any news here?
0
 

Author Comment

by:hadrons
ID: 2762997
I'm sorry for taking so long to get back to all of you (it was something beyond my control;) I'm working with Informix, and the MAX() fuction has been returning the desired results.

Thanks so much everyone for your help.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question