Solved

SQL Find correct record using  ;WITH

Posted on 2016-10-25
7
25 Views
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.

Filea

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 *
etc...
0
Comment
Question by:thayduck
  • 4
  • 3
7 Comments
 
LVL 65

Accepted Solution

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

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

Open in new window


< edited after original post to reflect correct answer >
0
 

Author Comment

by:thayduck
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.
With.PNG
WITH1.PNG
0
 
LVL 65

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.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:thayduck
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...
0
 

Author Comment

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

Expert Comment

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

Author Closing Comment

by:thayduck
ID: 41859203
Thanks again for the very quick response.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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