troubleshooting Question

optimizing a sql server query

Avatar of aturetsky
aturetsky asked on
Microsoft SQL Server 2005SQL
10 Comments5 Solutions289 ViewsLast Modified:
I have a table called customer_access that keeps track of what users have access to what customer.
I need to get a list of customer_access records that would give me the list of records where the approval_date is the latest for each of the user-customer combinations.

Currently, I am doing it this way, but I have a feeling that it can be done more elegantly.  I am looking to improve the clarity of the sql w/o hurting the performance (or, ideally, improving performance as well).

select * from (select row_number() over (partition by ca.customerId, ca.userId ORDER BY ca.approvalDate DESC) as rownumber, ca.id from customer_access ca) ca1 where ca1.rownumber = 1


ASKER CERTIFIED SOLUTION
reb73

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 5 Answers and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 5 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros