Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2000-03-25
8
Medium Priority
?
477 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 143

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

885 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