Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


SQL Find correct record using  ;WITH

Posted on 2016-10-25
Medium Priority
Last Modified: 2016-10-25
Using SQL  ;with , I need to only find the latest payment record with the highest payment amount for a customer.
I marked with a *, the records I need to be selected.

I will then take the With data and update another table.


Cust          Date                AMT
12345       2016/10/03   263,000
12345       2016/10/06   147,000
12345       2016/09/23    563,000  
12345       2016/10/06   153,000  *
12345       2016/10/06     99,000
23456       2016/08/02     15,000
23456       2016/09/13     11,000  *
456789     2016/10/10      66,300 *
564789     2016/03/12      65,000
564789     2016/03/12      65,500 *
Question by:thayduck
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
  • 4
  • 3
LVL 66

Accepted Solution

Jim Horn earned 2000 total points
ID: 41858775
Explain why the ;With is part of the requirement.

Give this a whirl..
;with cte as ( 
   SELECT Cust, Date, AMT, 
   FROM YourTable ) 
SELECT * FROM cte WHERE rank_order= 1 ORDER BY Cust

Open in new window

< edited after original post to reflect correct answer >

Author Comment

ID: 41859038
Your code executes but does not get the correct data for every customer.
For customer 144840, you got 2016/10/06 13,410 but it should have been 2016/10/06  251,578.

I will take that CTE file and update something with it.
LVL 66

Expert Comment

by:Jim Horn
ID: 41859158
>For customer 144840, you got 2016/10/06 13,410 but it should have been 2016/10/06  251,578.
You're going to have to speak to the sample data in the original question.  Keep in mind that experts here cannot connect to your data source(s) and run queries, so we are completely dependant on what you post and can only make educated guesses based on that.
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)


Author Comment

ID: 41859183
Sorry, that was just a example so you knew that it was not working right.
The samples I sent you all came back correct.

I made a small change (to your code)  below and that seemed to fix it.

changed lastamt  to  lastamt DESC

Now they all look right as far as I can see.

Thanks for your quick response...

Author Comment

ID: 41859186
Your code was actually  AMT (that is what I called it in my example) , I changed to AMT DESC.
LVL 66

Expert Comment

by:Jim Horn
ID: 41859189
>changed lastamt  to  lastamt DESC
Doh.  My bad, you are correct.  Max amount woud be DESC.

Author Closing Comment

ID: 41859203
Thanks again for the very quick response.

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

715 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