Solved

Special Join scenario

Posted on 2010-11-19
5
377 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
5 Comments
 
LVL 142

Assisted Solution

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

Expert Comment

by:jat0818
Comment Utility
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
Comment Utility
nvm,
pretty much what you had, did not see your entire statment.
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 250 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Stored procedure with a parameter 6 19
SQL Help joining two tables 7 33
SQL server 2008 SP4 29 31
Retention Policy for Backups 1 13
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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now