Solved

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

Posted on 2015-02-03
9
89 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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