Solved

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

Posted on 2000-03-25
8
427 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

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.

Question has a verified solution.

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

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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…

943 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

10 Experts available now in Live!

Get 1:1 Help Now