?
Solved

Special Join scenario

Posted on 2010-11-19
5
Medium Priority
?
389 Views
Last Modified: 2012-05-10
I'm trying to find a performant solution for a special JOIN, but don't know how to solve it. This JOIN needs lots of performance and is surely not optimal.

Here it is: For each customer (tblCustomer) there exists a colour, but this colour can change from day to day, and the history of all colours must be tracked. So we have:

tblCustomer: ID
tblColour: IDCustomer, colour, timestamp

What I need is the most current colour for the customer.

So my current JOIN goes like this:

 
SELECT tblCustomer.ID , C2.colour
FROM  tblCustomer (NOLOCK) JOIN tblColour AS C2 ON tblCustomer.ID = C2.IDCustomer 
          JOIN (SELECT C1.IDCustomer, MAX(C1.timestamp) AS MaxTime FROM tblColour AS C1 GROUP BY C1.IDCustomer) AS T1 ON (T1.MaxTime = C2.timestamp) AND (T1.IDCustomer = C2.IDCustomer)  

Open in new window


Any idea welcome
0
Comment
Question by:PC-Alex
[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
5 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 34173344
this article should help you solve the issue:
http://www.experts-exchange.com/A_3203.html
0
 

Expert Comment

by:jat0818
ID: 34175049
try this
select t2.ID,t3.Colour from
(
select t0.ID,max(t1.[timestamp]) as maxtime from tblCustomer t0
inner join tblColour t1 on t0.ID=t1.ID
group by t0.ID
) t2
inner join tblColour t3 on t2.ID=t3.ID
and t2.maxtime=t3.[timestamp]
0
 

Expert Comment

by:jat0818
ID: 34175108
nvm,
pretty much what you had, did not see your entire statment.
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 1000 total points
ID: 34178081
select C.ID, C2.Colour
from tblCustomer C
join(
      select IDCustomer, Colour, rn=row_number() over (partition by IDCustomer order by timestamp desc)
      from tblColour) C2 ON C.ID = C2.IDCustomer and C2.rn=1
0
 
LVL 1

Author Closing Comment

by:PC-Alex
ID: 34178611
Thank you guys; I split points, because angel was faster and brought some background, and cyberkiwi took the time to type the solution.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

771 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