Solved

SQL Query to find Latest Price

Posted on 2004-09-14
9
747 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 63 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

740 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