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

x
?
Solved

How to query the Last Transaction Per Item based on Identity Column?

Posted on 2015-02-03
9
Medium Priority
?
91 Views
Last Modified: 2015-02-04
I have a table with 5 columns that looks like as shown below. As I update the balance according to the RefNo the ID is incrementing.  How can I query them so that I can only get the last transaction pre RefNo?  

IdentityColumn (ID)      RefNo      Action      Status      Balance
12      3524AQSS25      A      OS      1500
14      HSGSTT152      A      OS      30000
13      32LJHHF455      A      OS      32500
16      3524AQSS25      A      PP      1000
17      HSGSTT152      A      FS      0
20      32LJHHF455      A      PP      20000
24      3524AQSS25      A      NS      500
31      32LJHHF455      A      PP      10000
                        
I'm hoping to extract this output from the above data                         
                        
Last Transaction Per RefNo                        
17      HSGSTT152      A      FS      0
24      3524AQSS25      A      NS      500
31      32LJHHF455      A      PP      10000


Any help would be greatly appreciated.
0
Comment
Question by:JimiJ13
[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
9 Comments
 
LVL 12

Expert Comment

by:Saurabh Bhadauria
ID: 40587990
your question is not clear to me ..
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40588010
Declare @MAxID int;
Select  @MAxID = Max(ID) From LogTable;
' here you do your update
Select * From LogTable Where ID > @MAxID;
0
 

Author Comment

by:JimiJ13
ID: 40588019
Saurabh Bhadauria,

In my sample, I have only 3 RefNo (could be many) just to illustrate what I want to get.  When I query I want to get only the last transaction of each RefNo.

I hope that makes sense.
0
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

by:JimiJ13
ID: 40588022
eghtebas,

Thanks for the quick response.  I hope you are not trying to give me the top 1. Anyway, I will try it and come back to you.


Thanks.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40588027
-- There is no top 1; put the following in a new query screen and run it after you do the necessary adjustments to it

Declare @MAxID int;                                                
Select  @MAxID = Max(ID) From LogTable;      -- capture the max ID that exists
GO
' here you do your update                                  -- put your update code here
GO
Select * From LogTable Where ID > @MAxID; -- Select fields you want to see

Change  LogTable to whatever you have.
0
 

Author Comment

by:JimiJ13
ID: 40588035
eghtebas,

Using your suggested code I got nothing.

If I do like this, I got the top 1.
Select * From LogTable Where ID = @MAxID;

I need to get the last transaction per RefNo. If I have 20 RefNo, I must get 20 as well.


Thanks.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40588036
Did you run this:

Declare @MAxID int;                                                
Select  @MAxID = Max(ID) From LogTable;


Select  @MAxID    -- <-- I tested this in SQL Server, it gave me the max ID before update which will be used later in

Select * From LogTable Where ID > @MAxID


If it works form me, there is no reason why it shouldn't for you.
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 40588148
row_number() works very well for finding the most recent rows.

select
*
from (
          select
          *
          , row_number() over (partition by RefNo order by id DESC) as rn
          from LogTable
          ) as derived
where rn = 1

note I have ordered by ID DESC, there may be other columns you prefer to order by (e.g. date_created).
0
 

Author Closing Comment

by:JimiJ13
ID: 40588366
Perfect!  Thanks PortletPaul.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 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