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

x
?
Solved

SQL Query to find Latest Price

Posted on 2004-09-14
9
Medium Priority
?
767 Views
Last Modified: 2008-02-01
Hi,

I have a table which holds a history of Fund Prices. Most Prices change on a daily basis, but this is not always the case. If a price has not changed then it will not get a new entry.

So... the question is.. how do I return a table of prices without doing nasty stuff like nested selects?

The table structure is as follows:

NAME                           EFFECTIVE_DATE                PRICE
=======================================
FISS                             2004-09-13                         2200
FISS                             2004-09-12                         2122
FIBB                             2004-09-12                         9800



I want to return a table that will show the latest price for each NAME. At the moment I'm doing it like this : (Which is really slow and i'm sure not the correct way)

Select NAME,PRICE
from Price_Table P1
Where Effective_Date = (Select Max(Effective_Date) from Price_Table P2 Where P1.NAME = P2.Name)

As the Price table is really big, this takes forever.


I am sure there must be a much better way to do this. Something along the lines of grouping the Name against the MAX(Effective_Date) or doing a TOP 1 within a group

Thanks



0
Comment
Question by:ggets
  • 3
  • 2
9 Comments
 
LVL 10

Expert Comment

by:AaronAbend
ID: 12055390
There is no better way to do this really.  Put an index on price_table:

create index datename on price_table(name, effective_date)

That should help a lot. If you already have that index, let me know. There are other tricks. The real question is where the performance is being degraded.
0
 

Author Comment

by:ggets
ID: 12055486
well.. actually there is no index on the table, and I won't be able to add one. This is not my database and I cannot change the structure (it is part of a much bigger system)
0
 
LVL 10

Accepted Solution

by:
AaronAbend earned 252 total points
ID: 12055590
Not good!

Here's another syntax to try...

Select NAME,PRICE
from Price_Table P1 join (select p2.name, Max(Effective_Date) as p2.maxdate from Price_Table P2 group by p2.name) on P1.NAME = P2.Name  
Where p1.Effective_Date = p2.maxdate

Put your SQL and this into the same query analyzer and analyze them at the same time and see if the strategies are different...
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 10

Expert Comment

by:AaronAbend
ID: 12055598
Note - there may be some syntax missing  in that -- the select list should have p1 prefixes.
0
 
LVL 8

Assisted Solution

by:MartinCMS
MartinCMS earned 248 total points
ID: 12055738
try this...

Select       yt.[NAME],Convert(varchar(10),yt.EFFECTIVE_DATE,101) as EFFECTIVE_DATE,PRICE
From       yourtable yt inner join yourtable ytj on
      yt.[NAME] = ytj.[NAME] and
      Convert(varchar(10),yt.EFFECTIVE_DATE,102) <= Convert(varchar(10),ytj.EFFECTIVE_DATE,102)
Group By yt.[NAME],yt.EFFECTIVE_DATE
Order By yt.[NAME],yt.EFFECTIVE_DATE
0
 

Author Comment

by:ggets
ID: 12398038
My Appologies for not getting back to this one. Have been away, sick, over worked etc etc etc etc...

Above suggestions make sence so happy that the points get allocated...

Many Thanks
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

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