Solved

Special Join scenario

Posted on 2010-11-19
5
386 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 250 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 250 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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