Solved

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

Posted on 2000-03-25
8
419 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now