Solved

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

Posted on 2015-02-03
9
81 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
9 Comments
 
LVL 12

Expert Comment

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

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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 33

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 33

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 48

Accepted Solution

by:
PortletPaul earned 500 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

16 Experts available now in Live!

Get 1:1 Help Now