Solved

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

Posted on 2000-03-25
8
444 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
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.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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…

820 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